繁体   English   中英

从其他类调用方法并使其保持非静态

[英]Calling method from a different class and keeping it non-static

我正在构建一个二十一点模拟器,以提高我的Java技能,并且在击中类时使类之间相互通信时遇到问题。

我正在创建一个人类类,其中将包含玩家和发牌者共有的方法,请尝试其中的一种。 我有一个Deck类,它创建一副纸牌,该类有一个方法getCard。 在Human中,我希望hit方法从Deck类调用getCard。

我不想使getCard成为静态对象,因为此方法需要从Deck类的实例变量的卡组中删除卡。 我也不想在Human类中创建甲板的新实例,因为我只希望每个游戏一个甲板,而不是每个人类一个甲板。

如何正确地做到这一点? 目前,IDE(Netbeans)表示“在Human的hit方法中,此行上找不到符号Method Deck(),

hand.add(Deck().getCard());

这是Deck类的getCard方法:

//Removes a random card from the deck and deletes it from the deck.
//It removes one card per call to the function.
    public Card getCard(){
        Random rand = new Random();
        int index = rand.nextInt(deck.size());

        Card toDeal = new Card (deck.get(index).getName(), 
                                deck.get(index).getSuit(), 
                                deck.get(index).getValue());

        deck.remove(index);

        return toDeal;
    }

这是人类课上的命中方法

public void hit(){
    hand.add(Deck().getCard());
}

如果我不包含任何内容,则同时包含两个类:

package blackjack;

import java.util.*;

public class Deck {

    private static int numSuits = 4;
    private static int numRanks = 13;
    private static int numCards = numSuits * numRanks;

    private ArrayList<Card> deck;

    public Deck() {

        String suit = null;
        String name = null;        
        int value = 0;
        deck = new ArrayList<Card>();

        for (int i=1; i<=numSuits; i++){
            for (int j=1; j <= numRanks; j++){
                switch (i){
                    case 1: suit = "Clubs"; break;
                    case 2: suit = "Diamonds"; break;
                    case 3: suit = "Hearts";  break;
                    case 4: suit = "Spades"; break;
                }

                switch (j){
                    case 1: name = "Ace"; value = 0; break;
                    case 2: name = "Two"; value = 2; break;
                    case 3: name = "Three"; value = 3; break;
                    case 4: name = "Four"; value =4; break;
                    case 5: name = "Five"; value = 5; break;                             
                    case 6: name = "Six"; value = 6; break;
                    case 7: name = "Seven"; value = 7; break;
                    case 8: name = "Eight"; value = 8; break;
                    case 9: name = "Nine"; value = 9; break;
                    case 10: name = "Ten"; value = 10; break;                           
                    case 11: name = "Jack"; value = 10; break;
                    case 12: name = "Queen"; value = 10; break;
                    case 13: name = "King"; value = 10; break;                           
                }

                Card card = new Card (name, suit, value);  
                deck.add(card); 
            }
        }
    }

    public int getSize(){
        return deck.size();
    }

    public void printDeck(){

        for (Card card : deck){
            System.out.println(card);
        }
    }

//Removes a random card from the deck and deletes it from the deck.
//It removes one card per call to the function.
    public Card getCard(){
        Random rand = new Random();
        int index = rand.nextInt(deck.size());

        Card toDeal = new Card (deck.get(index).getName(), 
                                deck.get(index).getSuit(), 
                                deck.get(index).getValue());

        deck.remove(index);

        return toDeal;
    }
}

和人类阶级(这是不完整的):

public class Human {
    private int handValue;
    private String name;
    private ArrayList<Card> hand;

    public Human(String name){
        this.handValue = 0;
        this.name = name;
        this.hand = null;
    }

    public void setName(String name){
        this.name = name;
    }

    public String getName(){
        return name;
    }

    public int getHandValue(ArrayList<Card> hand){

        for (Card card: hand){
            handValue += card.getValue();
        }

        return handValue;
    }

    public void hit(){
        hand.add(Deck().getCard());
    }


}

最后,是card类的构造函数:

public Card(String name, String suit, int value){
        this.name = name;
        this.suit = suit;
        this.value = value;
    }

您的问题是引用之一,您必须确保Human具有一个Deck字段,该字段引用正在使用的实际Deck,然后使hit()方法调用Deck实例的getCard()方法。 您可以在Human的构造函数中或setDeck(Deck deck) setter方法中将有效引用传递给实际的Deck。


顺便说一句,请注意,扑克牌通常被用作使用枚举的经典用例。 例如:

enum Rank {
   ACE("Ace", 1, 11), TWO("Two", 2, 2), THREE("Three", 3, 3), 
   FOUR("Four", 4, 4), FIVE("Five", 5, 5), SIX("Six", 6, 6), 
   SEVEN("Seven", 7, 7), EIGHT("Eight", 8, 8), NINE("Nine", 9, 9), 
   TEN("Ten", 10, 10), JACK("Jack", 10, 10), QUEEN("Queen", 10, 10), 
   KING("King", 10, 10);

   private int value1;
   private int value2;
   private String name;

   private Rank(String name, int value1, int value2) {
      this.value1 = value1;
      this.value2 = value2;
      this.name = name;
   }

   public int getValue1() {
      return value1;
   }

   public int getValue2() {
      return value2;
   }

   public String getName() {
      return name;
   }

}

enum Suit {
   CLUBS, DIAMONDS, HEARTS, SPADES
}

class Card {
   private Rank rank;
   private Suit suit;

   public Card(Rank rank, Suit suit) {
      this.rank = rank;
      this.suit = suit;
   }

   public Rank getRank() {
      return rank;
   }

   public Suit getSuit() {
      return suit;
   }

}

class Deck {
   List<Card> cards = new ArrayList<>();

   public Deck() {
      for (Suit suit : Suit.values()) {
         for (Rank rank : Rank.values()) {
            cards.add(new Card(rank, suit));
         }
      }
      Collections.shuffle(cards); // shuffle them
   }

   // other methods including deal here
}

您尚未创建Deck的任何实例。

使用new Deck()某个地方创建Deck实例。

然后,您可以通过以下方式共享对此一个Deck的引用:

  • 创建一个类Game ,并在其中创建一个Deck并将对它的引用传递给Human实例。
  • 使Deck成为单例课程。 [注意:单例经常被皱眉-如果您以后想在同一个应用程序中操纵多个卡组该怎么办?]

例如,这是将Deck引用传递给Human

    class Human {
          ...
          private Deck deck;

          public Human( String name, Deck deck ) {
              ...
              this.deck = deck;
          }

然后是实现hit()

          public void hit(){
              hand.add( deck.getCard());
          }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM