[英]private final Card[]cards, how do I use this in another class
Private final Card[] cards;
Public Deck() {
this.cards = new Card[52];
int i = 0;
for (Suit suit : Suit.values()) {
for (Rank rank : Rank.values()) {
cards[i++] = new Card(rank, suit);
}
}
}
This is an array of cards it works in the class it is made called Deck.这是一组卡片,它在名为 Deck 的类中起作用。 I am trying to make a card game that implements this along with other classes.
我正在尝试制作一个与其他类一起实现这一点的纸牌游戏。 I want the Deck class to be reusable so it works in any card game.
我希望 Deck 类是可重用的,因此它可以在任何纸牌游戏中使用。 I want a Deck of cards essentially.
我基本上想要一副纸牌。 Now I know it works because I can use the
printDeck()
method I have in the same class to print off a deck of cards but don't know how or even if I can call it in a card game.现在我知道它有效,因为我可以使用我在同一个类中的
printDeck()
方法打印一副纸牌,但不知道如何或什至我是否可以在纸牌游戏中调用它。 Why would I need a getter when the new operator creates an instance of Deck which is essentially an array of Card [52] cards?当 new 操作符创建一个本质上是 Card [52] 卡片数组的 Deck 实例时,为什么我需要一个 getter? I want this Deck to be reusable so I don't have to rewrite it for every different type of card game.
我希望这套牌可以重复使用,这样我就不必为每种不同类型的纸牌游戏重写它。
You can simply define a getter method in your Deck
class as below :您可以简单地在
Deck
类中定义一个 getter 方法,如下所示:
public Card[] getCards() {
return cards;
}
Them from another class you can use this getter method once you have a Deck
object :他们来自另一个类,一旦你有一个
Deck
对象,你就可以使用这个 getter 方法:
Card[] cards = yourDeckObject.getCards();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.