【发布时间】:2017-09-14 02:30:31
【问题描述】:
我在创建二十一点游戏时遇到了学校作业的困难。 我们需要从接口中的面值在类中应用分值。我想办法是直接通过写 Ace(1),Two(2)... 来修改枚举,但是我们不允许修改接口。还有其他方法可以应用该值吗?
public interface PlayingCard
public enum Value
{
Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King
};
public class PlayingCardImp implements PlayingCard {
private Suit suit;
private Value value;
public PlayingCardImp (Value value, Suit suit)
{
this.value = value;
this.suit = suit;
}
@Override
public int getScore()
{
@return the score value of this card (Ace=1, J, Q, K=10, All others int of face value)
return 0;
}
}
【问题讨论】:
-
您也可以使用
Map<Value, Integer>的 Map 在枚举和卡片值之间创建一种关系。 (尽管它可能不是最好的方法) -
一种简单的方法是打开枚举值并在那里定义您的退货案例。
标签: java class enums interface