我想創建一個具有不同屬性的類的列表,但是我面臨一個問題:當我添加一個新類時,所有其他類都被修改(代碼非常大所以我張貼只是一個小樣本)Java創建一個包含多個對象的類的列表
public abstract class A{
public static Point Position;
public static int LifePoints;
public static int range;
public static int atackValue;
public static double movementSpeed;
public double getMovementSpeed() {
return movementSpeed;
}
public static void setMovementSpeed(double movement) {//this n funciona
movementSpeed = movement;
}
(...)to make the topic shorter i only show the movement part but class A contains all getters and setters for all movement,position,range,life points and attack value
}
public class B extends A{
public static Point Position;
public static int LifePoints=10000;
public static int range=50;
public static int atackValue=100;
public static double movementSpeed=2;
public A(Point startPoint){
setMovementSpeed(movementSpeed);
setAtackValue(atackValue);
setRange(range);
setLifePoints(LifePoints);
setPosition(startPoint);
}
public class createB(){
(...)
public void create(){
private List<B> list = new ArrayList<B>();
B b = new B(startpoint);
list.add(b);
}
(...)
for(int x=0;x!=list.length();x++){
move(list.get(x));
system.out.println(list.get(x).getMovementSpeed());
}
中的println返回的創建所有的b正確的值(2.0),但每次我創建一個新的b中的所有其他b的停止不動,新的b遷移的速度比前一個。移動功能是由我的老師製作的,並且已被證實能正常工作。我懷疑這個位置不能正常工作,而不是將圖片移動2個像素,它只移動一次X。
你的代碼有一個急性「靜態itis」的情況。請擺脫大部分靜態調節器,以免患者死亡。 – 2013-05-05 20:17:26
從字段中移除靜態 – user1121883 2013-05-05 20:18:01
另外,考慮清理您的代碼示例。這些領域中的大多數與您的問題無關(或者更確切地說,是同一基本問題的重複表現形式),而且這些領域並沒有縮進。 – millimoose 2013-05-05 20:19:36