简体   繁体   English

private final Card[]cards,我如何在另一个类中使用它

[英]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.

相关问题 如何使用PowerMockito在具有私有构造函数的类中设置原始私有静态final字段? - How do I set a primitive private static final field in a class with private constructor using PowerMockito? 我如何使用jMockit模拟标记为final并具有私有构造函数的类 - How do I mock a class marked final and has a private constructor using jMockit 如何在Java中使用或创建私有类的变量 - How do I use or create a variable of a private class in Java 如何传递私有final类的引用 - how to pass the reference of private final class 我该如何模拟通过Powermock和Mockito通过私有构造函数初始化的私有static final字段? - How do I mock a private static final field initialized via a private constructor using Powermock and Mockito? 如何使用PowerMockito从最终的静态类返回模拟对象 - How do I use PowerMockito to return a mock object from a final static class 如何在另一个类中使用静态类中的变量? - How do I use a variable from a static class in another class? 在类的私有静态最终变量中使用动态类名 - Use dynamic class name in private static final variable of a class 绑定元素时如何从netbeans创建的另一个类访问私有列表 - How do I access the private lists from another class created by netbeans when i bind elements 如何使用滑动手势删除回收视图中的卡片? - How do I use the swipe gesture to remove cards within a recycleview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM