gpt4 book ai didi

java - 如何比较java继承中的两个值?

转载 作者:行者123 更新时间:2023-12-01 09:09:55 25 4
gpt4 key购买 nike

我正在尝试创建一个程序来检查哪个帐户的成本最低,但我不知道如何在我的程序中实现这一点。

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

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

if (BronceDayTimeCost < SilverDayTimeCost){ System.out.print("\nThe Bronze Account is cheapest.");}

if (SilverDayTimeCost< BronceDayTimeCost ){ System.out.print("\nThe Silver Account is cheapest.");}

铜级

    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);
}
}

最佳答案

您可以将 calcDayTimeCost 方法的 return 类型收集到一个 double 变量中,如下所示,然后使用 检查哪个成本较低。 code>ifelse 条件。

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.");
}

尽管您的代码可以工作,但它与面向对象编程并不内联,即,一般来说,我们创建与现实世界对象内联的类,但是BronzeSilver两个不同的元素,即它们不直接相互关联/继承。

换句话说,当你说 ClassA extends ClassB 时,你是在说 ClassA IS-A B类

因此,替代方法如下:

  public abstract class Metal {

//add fields applicable for all metals

public double calcDayTimeCost() {
//add code here
}
}

public class Bronze extends Metal {

//Bronze specific details
}

public class Silver extends Metal {

//Silver specific details
}

关于java - 如何比较java继承中的两个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40984196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com