//Note that you declare this method static: as such it will have no access to object
//member fields, only to class fields: these are member fields declared 'static'
public static void calculatePrice(){
Scanner userInput = new Scanner(System.in);//Note this has little to do with actually calculating price.
int orderNumber;
double totalBill = 0;//You a declaring a new variable that will disappear once it goes out of the scope of this method
orderNumber = userInput.nextInt();
switch(orderNumber){
case 1:
totalBill = totalBill + American;
displayMenu();
calculatePrice();//Recursive call!
break;
//... eliding for brevity
case 0:
System.out.println("Your total bill will be $"+ totalBill);
break;
}
} //this is the point where all method variables go out of scope, because this 'stack frame' is unloaded. Any subsequent call to this method will not 'remember' any state that was not saved elsewhere.
因此,请注意您正在进行递归调用。这意味着你的重新输入你的功能。你的'totalBill'变量被新实例化,掩盖了其他所有'totalBill'变量。这些变量仍然存在......只是在较低的堆栈帧上无法访问。把栈帧看作一个新的表格,在这个表格上运行你的函数。当你调用一个新的函数时,一个新的表格被放置在顶部......但你只能看到顶部的表格。所有其他表都在它下面。
通过声明变量static,这意味着您将其设置在一边,以便此类型的所有对象中的所有函数都可以访问相同的内存空间。 static
变量可用于该类的所有对象;因此它们通常被称为“类字段”,而非静态成员变量则被称为“对象字段”。你的类签名应,在一个精心设计的世界是这样的:
public class RegisterOrder {
public double totalBill;//Available to only ONE instance (object) of this class.
public void calculatePrice() {//note no 'static' modifier!
//some code
}
}
这可能不会与您目前正在呼唤你calculatePrice功能的工作方式,因为当前的方法是static
。你必须要改变,要:
RegisterOrder order = new RegisterOrder();
order.calculatePrice();
注意,那就是,你的递归调用意味着,如果你为了很多很多的订单程序将会崩溃。把它看成堆叠太高的表。在现代系统中,这是很难做到的(大量的内存),但良好的设计要求这里的循环:
public function getOrder() {
//instantiate variables
orderNumber = userInput.nextInt();
while (orderNumber != 0) {
switch(orderNumber){
case 1:
totalBill = totalBill + American;
break;
case 2:
totalBill = totalBill + Expresso;
break;
case 3:
totalBill = totalBill + Latte;
break;
default:
//Error handling is important!
break;
}
displayMenu();
orderNumber = userInput.nextInt();
}
System.out.println("Your total bill will be $"+ totalBill);
}
注意,在这个版本中,你不能再呼叫您的输入功能。还要注意,在此函数中声明的'totalBill'变量不会被另一个对此函数的调用卸载或屏蔽。在真正干净的代码中,你可以将你的输入获取方法与账单计算方法分开,但是步骤很简单。 :)
这是关于范围。 –
对,我需要回到那一章。我认为只要它在同一个街区就可以。不过谢谢!我至少可以回头重读:D – Gibbo