我是java的新手,但我的理解是关键字new
出现在构造函数之前。但是,在Oracle Java教程的这个例子中,情况并非如此。
public static int numSuits = 4;
public static int numRanks = 13;
public static int numCards = numSuits * numRanks;
private Card[][] cards;
public Deck() {
cards = new Card[numSuits][numRanks];
for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++) {
for (int rank = Card.ACE; rank <= Card.KING; rank++) {
cards[suit-1][rank-1] = new Card(rank, suit);
}
}
}
卡片是课堂,我不明白这句话的含义:
cards = new Card[numSuits][numRanks];
有人可以解释这行代码的含义。
答案 0 :(得分:0)
在这种情况下,new Card[numSuits][numRanks]
实例化二维卡片阵列,而不是卡片类本身。实例化Card类后来,在2 for循环内部为new Card(rank, suit);