//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
변수는 해당 클래스의 모든 객체에서 사용할 수 있습니다. 그러므로 이들은 종종 '클래스 필드'라고 불리는 반면 비 정적 멤버 변수는 '객체 필드'라고합니다. 현재의 방법은 static
때문에 이것은 아마, 당신은 현재 calculatePrice 함수를 호출하는 방식으로 작동하지 않습니다
public class RegisterOrder {
public double totalBill;//Available to only ONE instance (object) of this class.
public void calculatePrice() {//note no 'static' modifier!
//some code
}
}
: 같은 클래스의 서명은, 잘 설계된 세계에 보일 것입니다.
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