我想創建一個程序,檢查哪個帳戶成本最低,但我不知道如何在我的程序中實現此目的。我該如何比較java中繼承的兩個值?
我要選擇的每類的輸出分別比較(dayTimeCost)
這就是我認爲這可能是implemted,但我不知道該怎麼
如果(BronceDayTimeCost < SilverDayTimeCost){是System.out.print( 「\ n該青銅帳戶是最便宜的。」);}
如果(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);
}
}
歡迎堆棧溢出!看起來你正在尋求作業幫助。雖然我們本身沒有任何問題,但請觀察這些[應做和不應該](http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions/338845#338845),並相應地編輯您的問題。 –
您應該閱讀更多關於繼承的內容。銀牌不應該繼承銅牌。它應該繼承。 (爲了示範而不是需要)從更抽象的東西,比如關卡。 – DejaVuSansMono