随机播放方法不起作用交换值?

时间:2016-04-08 06:28:33

标签: java inheritance

让我有一个名为 Card.java 的课程:

public class Card{
    private int face, suit;
    ...
    public void setFace(int newFace){
        face = newFace;
    }
    public void setSuit(int newSuit){
        suit = newSuit;
    }
}

DeckOfCards.java

public class DeckOfCards extends Card{
    public Card[] newDeck = new Card[52];
    public void Shuffle(){


    }
    main(){
        DeckOfCards deck = new DeckOfCards();
        deck.Shuffle();
    }
}

洗牌

   public void Shuffle(){                                    //Shuffles 52 times
      Random ran = new Random();
      int tmp_face, tmp_suit;
      int card1_idx;
      int card2_idx;
      for (int i = 0; i < 10; i++) {
         card1_idx = ran.nextInt(52);
         card2_idx = ran.nextInt(52);
         tmp_face = newDeck[card1_idx].getFace();
         tmp_suit = newDeck[card1_idx].getSuit();
         System.out.println(" Inx1 is " + newDeck[card1_idx].getFace());
         System.out.println(" Ind2 is " + newDeck[card2_idx].getFace());
         newDeck[card1_idx].setFace(newDeck[card2_idx].getFace());
         newDeck[card1_idx].setSuit(newDeck[card2_idx].getSuit());
         newDeck[card2_idx].setFace(tmp_face);
         newDeck[card2_idx].setSuit(tmp_suit);
         System.out.println(" Swapped Ind1 is " + newDeck[card1_idx].getFace());
         System.out.println(" Swapped Ind2 is " + newDeck[card2_idx].getFace());
      }
   } 

在我所有的deck.Shuffle()之后,我注意到当我打印它们时,牌组被洗牌,它们在洗牌之前仍然处于相同的顺序。这会与继承有关吗?子类方法可以修改其父类的数据类型的成员数据吗?

0 个答案:

没有答案