目前我有以下代码(类Car
和一个名为test
的临时类来测试前者:
class test {
public static void main(String[] args) {
Car[][] volvo = new Car[3][3];
volvo[0][0] = new Car(300, 200);
volvo[0][1] = new Car(250, 250);
System.out.println("Car 1 Weight/Height" + volvo[0][0].returnSize() + "/" + volvo[0][0].returnWeight());
System.out.println("Car 2 Weight/Height" + volvo[0][1].returnSize() + "/" + volvo[0][1].returnWeight());
}
}
class Car {
private static int size;
private static int weight;
Car(int carSize, int carWeight) {
size = carSize;
weight = carWeight;
}
public static int returnSize() {
return size;
}
public static int returnWeight() {
return weight;
}
}
我从前面提到的片段获得的输出:
Car 1 Weight/Height250/250
Car 2 Weight/Height250/250
我真的希望得到的输出:
Car 1 Weight/Height300/200
Car 2 Weight/Height250/250
有没有理由为第一个创建的对象(new Car(300, 200)
)被第二个对象完全覆盖,有没有办法解决这个问题?