我有这个类使用枚举类型作为甲板。使此类 Comparable 将卡片按升序排序的最有效方法是什么?如果它们是枚举类型,是否可以在一个方法中比较两个卡面值?(例如返回差异或什么?)谢谢。
public class Card {
public enum Rank {ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN,
JACK, QUEEN, KING}
public enum Suit {CLUBS, DIAMONDS, HEARTS, SPADES}
private final Rank rank;
private final Suit suit;
private Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
public int compareTo(Card c) {
Card aCard = (Card)c;
int rankComparison = rank.compareTo(aCard.rank);
return rankComparison != 0 ? rankComparison : suit.compareTo(aCard.suit);
}
public Rank rank(){
return rank;
}
public Suit suit(){
return suit;
}
public String toString(){
return "The " + rank + " of " + suit + ".";
}
}