如何在java中继承继承中的两个值?

时间:2016-12-05 22:04:41

标签: java inheritance

我正在尝试创建一个程序来检查哪个帐户的费用最低,但我不知道如何在我的程序中实现此功能。

我想分别选择每个类的输出来比较(dayTimeCost)

我认为这可能是实施的,但我不知道如何

  

if(BronceDayTimeCost< SilverDayTimeCost){System.out.print(" \ n青铜帐户最便宜。");}

     

if(SilverDayTimeCost< BronceDayTimeCost){System.out.print(" \ n银帐户最便宜。");}

青铜班

    public  class Bronze  {

    // ----------------- Atributes -----------------------



    public int dayMinutes;      // daytime telphone minutes used

    public double dayTimeCost;  //Total daytime calls cost

    // ------------- CONSTRUCTORS (inputs) ---------------

    public Bronze(int theDayMinutes ) {  
    dayMinutes = theDayMinutes; 
    }
    // ------------------ METHODS ------------------------
    // Calculate Total daytime calls cost
    public double calcDayTimeCost(double costDay) {

        dayTimeCost = dayMinutes * costDay;
        System.out.print("\nCost of daytime calls = "  + costDay + "/min"+
        "\n\nTotal daytime calls cost = " + dayTimeCost + 
        "\n");

        return dayTimeCost;
    }

    //toString method to override that in Object
    public String toString(){
    return(
 "\n" 
         );
    }

    //Returns the type of account
    public String type(){
    return "Bronze";
    }
}

银级

   public class Silver extends Bronze {

    private static final double costDay = 0.22;

    public Silver(int theDayMinutes ) {
    super(theDayMinutes );

  }
    //Returns the type of account
    public String type(){
    return "Silver";
    }

}

主要类

    import java.util.Scanner;

public class AccountUser {

    // ------------------- FIELDS ------------------------    

    // Create instance of Scanner class
    public static Scanner input = new Scanner(System.in);
    // variables
    public static Bronze bron;
    public static Silver silv;

    public static int dayMinutes;

    // ------------------ METHODS ------------------------  

    public static void main(String [] args) {

        // Input dayMinutes (with error message)
        do{
        System.out.print("Please daytime telphone minutes used --> ");
        dayMinutes  = input.nextInt();
        if  ( dayMinutes <= 0){System.out.print("\n" + "Input value outside the range!!!" + "\n");}
        }while( dayMinutes <= 0);


        // Create new Bronze instance
        bron = new Bronze(dayMinutes);
        silv = new Silver(dayMinutes);
        // Calculate scheme1, scheme2
        bron.calcDayTimeCost(0.12);
        silv.calcDayTimeCost(0.22);


    System.out.println(bron);
    System.out.println(silv);
    }
}

1 个答案:

答案 0 :(得分:1)

您可以将return类型的calcDayTimeCost方法收集到double变量中,如下所示,然后使用if和{{1}检查哪个费用较低条件。

else

尽管您的代码有效,但它不符合面向对象编程,即,通常,我们创建与真实世界对象内联的类,但是double bronCost = bron.calcDayTimeCost(0.12); double silverCost = silv.calcDayTimeCost(0.22); if(bronCost == silverCost) { System.out.print("\nThe Silver and Bronze Accounts are same cost "); } else if (bronCost < silverCost){ System.out.print("\nThe Bronze Account is cheapest."); } else { System.out.print("\nThe Silver Account is cheapest."); } Bronze两个不同的元素,即它们#39; t直接相互关联/继承。

换句话说,当您说Silver ClassA extends时,您说的是ClassB IS-A ClassA

因此,替代方法如下:

ClassB