在Java中,如何在調用函數中獲得多個基本變量的累計總數。我想用另一種方法來進行添加。但是我怎樣在Java中做它,因爲它通過值來傳遞原始類型?Java - 調用方法的累計總數 - 按值調用
public void methodA(){
int totalA = 0;
int totalB = 0;
Car aCar = getCar() ; //returns a car object with 2 int memebers a & b
methodB(aCar);
methodB(bCar);
methodB(cCar);
sysout(totalA); // should print the sum total of A's from aCar, bCar and cCar
sysout(totalB); // should print the sum total of b's from aCar, bCar and cCar
}
private methodB(aCar){
totalA += aCar.getA();
totalB += aCar.getB();
}
可能重複: http://stackoverflow.com/questions/2832472/how-to-return-2-values-from-a-java-function – 757071 2012-08-07 03:01:15
Java沒有 「呼叫通過參考」 ..但這甚至不試圖顯示/模擬。 – 2012-08-07 03:04:00