从不同的类调用方法并使其保持非静态

时间:2014-07-17 20:49:08

标签: java methods static

我正在构建一个二十一点模拟器来提高我的java技能,并且在遇到命中时遇到问题让我们互相交流。

我正在创建一个Human类,它将包含玩家和经销商共有的方法,并成为其中之一。我有一个类Deck,它创建了一副牌,这个类有一个方法getCard。在Human中,我希望hit方法从Deck类调用getCard。

我不想让getCard静态,因为这个方法需要从甲板上移除一张卡片,它是Deck类的实例变量。我也不想在Human类中创建一个新的deck实例,因为我每场只需要一个牌组,而不是每人一个牌组。

如何正确完成?现在,IDE(Netbeans)说"在人类的命中方法中找不到符号,方法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;
    }

这是来自Human class的命中方法

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());
    }


}

最后,来自卡类的构造函数:

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

2 个答案:

答案 0 :(得分:3)

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


另外,请注意,扑克牌通常用作使用枚举的经典用例。例如:

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
}

答案 1 :(得分:3)

您尚未创建任何甲板实例。

使用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());
          }