Java Eclipse 建议我将我的方法设为静态
Java Eclipse suggests that I make my method static
我有 类 Player
、PoliticCard
和 ModelPlayer
。 Player
和 PoliticCard
都有一个 ArrayList
来存储来自 enum Color
的颜色值。我想编写方法 DrawCard()
,它从 PoliticCard
的 ArrayList
中获取 Color
值并将其存储在 Player
的 ArrayList
中。
Eclipse 让我将 PoliticCard
的 ArrayList
更改为 static
但在其他所有 post 中我都看到 static
仅用于常量这里不是这种情况(每次每个玩家使用方法 DrawCard()
时 PoliticCard
中的 ArrayList
都会不断变化)所以 [=13] 中有一个静态 ArrayList
=] 这里会有问题吗?
PS:类全部在不同的包里(当然需要的时候我都导入了),跟这个有关系吗?
public enum Color {
RED, GREEN
}
public class PoliticCard {
private Color cardColor;
private static ArrayList<Color> politicCards;
public PoliticCard() {
int i = 0;
while (i < 13) {
politicCards.add(Color.RED);
i++;
}
while (i < 26) {
politicCards.add(Color.GREEN);
i++;
}
}
public static ArrayList<Color> getPoliticCards() {
return politicCards;
}
}
public class Player {
private int id;
private ArrayList<Color> politicCards;
public Player(int id) {
this.setId(id);
}
public ArrayList<Color> getPoliticCards() {
return politicCards;
}
}
public class ModelPlayer {
public void Player(Player player) {
int i = 0;
while (i < 10) {
new Player(i);
}
}
public void DrawCard(Player player) {
int i = 0;
if (PoliticCard.getPoliticCards().get(i) != null) {
player.getPoliticCards().add(PoliticCard.getPoliticCards().get(i));
PoliticCard.getPoliticCards().remove(i);
} else {
i++;
player.getPoliticCards().add(PoliticCard.getPoliticCards().get(i));
PoliticCard.getPoliticCards().remove(i);
}
}
}
Eclipse made me change the arrayList of PoliticCard to static
不是 Eclipse 迫使您更改为静态,而是 Java 编译器。这背后的原因是你的 DrawCard 方法中的这一行:
PoliticCard.getPoliticCards().remove(i);
您正在尝试使用 class PoliticCard 而不是 PoliticCard 实例访问 getter 政治卡片。这就是为什么 getter 和字段都必须是静态的。
我有 类 Player
、PoliticCard
和 ModelPlayer
。 Player
和 PoliticCard
都有一个 ArrayList
来存储来自 enum Color
的颜色值。我想编写方法 DrawCard()
,它从 PoliticCard
的 ArrayList
中获取 Color
值并将其存储在 Player
的 ArrayList
中。
Eclipse 让我将 PoliticCard
的 ArrayList
更改为 static
但在其他所有 post 中我都看到 static
仅用于常量这里不是这种情况(每次每个玩家使用方法 DrawCard()
时 PoliticCard
中的 ArrayList
都会不断变化)所以 [=13] 中有一个静态 ArrayList
=] 这里会有问题吗?
PS:类全部在不同的包里(当然需要的时候我都导入了),跟这个有关系吗?
public enum Color {
RED, GREEN
}
public class PoliticCard {
private Color cardColor;
private static ArrayList<Color> politicCards;
public PoliticCard() {
int i = 0;
while (i < 13) {
politicCards.add(Color.RED);
i++;
}
while (i < 26) {
politicCards.add(Color.GREEN);
i++;
}
}
public static ArrayList<Color> getPoliticCards() {
return politicCards;
}
}
public class Player {
private int id;
private ArrayList<Color> politicCards;
public Player(int id) {
this.setId(id);
}
public ArrayList<Color> getPoliticCards() {
return politicCards;
}
}
public class ModelPlayer {
public void Player(Player player) {
int i = 0;
while (i < 10) {
new Player(i);
}
}
public void DrawCard(Player player) {
int i = 0;
if (PoliticCard.getPoliticCards().get(i) != null) {
player.getPoliticCards().add(PoliticCard.getPoliticCards().get(i));
PoliticCard.getPoliticCards().remove(i);
} else {
i++;
player.getPoliticCards().add(PoliticCard.getPoliticCards().get(i));
PoliticCard.getPoliticCards().remove(i);
}
}
}
Eclipse made me change the arrayList of PoliticCard to static
不是 Eclipse 迫使您更改为静态,而是 Java 编译器。这背后的原因是你的 DrawCard 方法中的这一行:
PoliticCard.getPoliticCards().remove(i);
您正在尝试使用 class PoliticCard 而不是 PoliticCard 实例访问 getter 政治卡片。这就是为什么 getter 和字段都必须是静态的。