我遇到继承问题。卡片组,继承问题?
基本上我想教自己面向对象的编程,我不能让我的枚举数组使用我为它创建的toString
方法。而是使用PlayingCards
类中的那个。
我该如何解决这个问题?
public class PlayingCards {
private final Rank rank;
private final Suit suit;
/**
* Creates enums for rank
*/
public static enum Rank {
ACE(1),
TWO(2),
THREE(3),
FOUR(4),
FIVE(5),
SIX(6),
SEVEN(7),
EIGHT(8),
NINE(9),
TEN(10),
JACK(11),
QUEEN(12),
KING(13);
private int rank;
private Rank(int rank) {
this.rank = rank;
}
public int getRank() {
return this.rank;
}
}
/**
* Creates enums for suit
*/
public static enum Suit {
HEARTS(14),
SPADES(15),
CLUBS(16),
DIAMONDS(17);
private int suit;
private Suit(int suit) {
this.suit = suit;
}
public int getSuit() {
return suit;
}
}
/**
* Constructs a card with specified initial rank and suit
*
* @param rank sets rank to ACE by default
* @param suit sets suit to SPADES by default
*/
public PlayingCards(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
public String toString() {
return getClass().getName() + "[Rank = " + this.rank
+ ", Suit = " + this.suit + "]" + "\n";
}
public String format() {
return this.rank + " of " + this.suit;
}
/**
* Tests whether this card is equal to some other card.
*
* @param otherObject the card to be tested.
* @param rank imports enums rank
* @param suit imports enums suit
* @return returns true if the suit and rank of test card is equal to the
* suit and rank of this card otherwise false is returned.
*/
public boolean equals(Object otherObject, Rank rank, Suit suit) {
if (otherObject == null) {
return false;
}
if (getClass() != otherObject.getClass()) {
return false;
}
PlayingCards other = (PlayingCards) otherObject;
return suit == other.suit && rank == other.rank;
}
}
这部分是我遇到问题的地方。它正在创建数组,但它只是在错误的类中使用toString
方法在编译器中显示它。
import java.util.Arrays;
public class Pack extends PlayingCards
{
static PlayingCards[] card = new PlayingCards[52];
PlayingCards.Suit[] suit2 = PlayingCards.Suit.values();
PlayingCards.Rank[] rank2 = PlayingCards.Rank.values();
private int numberOfCards;
/**
* Constructs a pack of 52 cards.
* Sorted by suit Clubs, Diamonds, Hearts, Spades.
* Sorted ascending.
* @param rank
* @param suit
*/
public Pack(Rank rank, Suit suit)
{
super(rank,suit);
card = new PlayingCards[52];
numberOfCards = 0;
for (int x = 0; x < suit2.length; x++)
{
for (int y = 0; y < rank2.length;y++)
{
card [numberOfCards] = new PlayingCards(rank2[y],suit2[x]);
numberOfCards ++;
}
}
}
/**
* Shuffles cards in pack.
*/
public void shuffle()
{
}
/**
* @return string representation of 52 card pack.
*/
@Override
public String toString() {
String toString = "New pack\n";
for (int cards =0; cards < card.length; cards++)
{
toString = toString + card[cards] + "\n";
}
return toString;
}
}
这就是我得到的。结果已经使用了错误的方法toString
格式:
assed3.PlayingCards[Rank = ACE, Suit = HEARTS]
, assed3.PlayingCards[Rank = TWO, Suit = HEARTS]
, assed3.PlayingCards[Rank = THREE, Suit = HEARTS]
, assed3.PlayingCards[Rank = FOUR, Suit = HEARTS]
, assed3.PlayingCards[Rank = FIVE, Suit = HEARTS]
你可以打破你的代码分开,你遇到问题的部分(并添加你期待什么)从类定义的其余部分(也许去除共无关代码) –