我目前正在做我的学期作业。我正在设计一个食物订购系统,该系统允许用户自己订购食物和饮料。
我有两堂课,其中一堂是点菜,另一堂是点饮料。
现在,问题是我无法将下面的薪水变量即食物总金额传递到订购饮料类中以计算总金额。因此,我想知道是否可以将薪水转嫁给其他班级?
for(int i=0;i<list_of_food.size();i++) {
food = list_of_food.get(i);
report += food.toString();
double totalprice_of_food = food.getQuantity() * food.getPrice();
pay += totalprice_of_food;
report = report + " - "+totalprice_of_food+"\n";
}
答案 0 :(得分:0)
为什么需要将变量传递给另一个类。
创建3个类:
FoodOrder
DrinksOrder
FinalBil
FinalBill will call your FoodOrder and DrinksOrder both to fetch the individual amount
and calculate the Total-Bill amount.
按如下所示创建您的设计:
Class FoodOrder {
public double calculateFoodBill(){
// Calculate and return the food bill amount
}
}
Class DrinksOrder {
public double calculateDrinksBill(){
// Calculate and return the drinks bill amount
}
}
Class FinalBill {
public static void main(String[] args){
FoodOrder fo = new FoodOrder ();
DrinksOrder do = new DrinksOrder ();
double foodTotal = fo.calculateFoodBill();
double drinksTotal = do.calculateDrinksBill();
double totalBillAmount = foodTotal + foodTotal;
}
}
答案 1 :(得分:0)
您可以通过类构造函数将变量从一个类传递到另一个类。 (希望您对构造函数熟悉,否则请在youtube上查看。) 很乐意提供帮助:)