我有一个名为 DeckPC 的类:
public class DeckPC {
// creating 3 cards, composed of one random number from 1 to 7
private int cardSword = (int)(Math.random() * ((7 - 1) + 1)) + 1;
private int cardBast = (int)(Math.random() * ((7 - 1) + 1)) + 1;
private int cardGold = (int)(Math.random() * ((7 - 1) + 1)) + 1;
static List<Integer> DeckPC = new ArrayList<Integer>();
public void creatingDeck(int cardSword, int cardBast, int cardGold) {
this.cardSword = cardSword;
this.cardBast = cardBast;
this.cardGold = cardGold;
// trying to add the variables(ints) from above into the DeckPC list.
DeckPC.add(cardSword);
DeckPC.add(cardBast);
DeckPC.add(cardGold);
}
public List<Integer> getDeckPC() {
return DeckPC;
}
}
然后是一个带有 main 方法的主类,我想在其中调用 getDeckPC() 以便它显示我插入到 DeckPC 中的值:
DeckPC deckPC = new DeckPC();
deckPC.getDeckPC();
但问题是它根本不会以这种方式返回任何东西。但是,如果我在 createDeck 方法中初始化列表“DeckPC”(没有静态),那么它会返回一个列表,但为空,如下所示:[]。我究竟做错了什么?也许使用了错误的访问修饰符?