在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();
}