差异法java使用枚举类型 [英] difference method java using enum types
本文介绍了差异法java使用枚举类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我正在学习枚举并且不确定实现它的最有效方式时,我感到困惑。
public class卡片可比较卡> {
public enum等级{两,三,四,五,六,七,七,十,十八$ b JACK,女王,王,ACE}
公开枚举西装{CLUBS,DIAMONDS ,HEARTS,SPADES}
public static int difference(Card c){
}
$
解决方案
public enum Rank {ACE(1),TWO(2); / *显然添加其他值* /
private int value;
Rank(int value){
this.value = value;
};
public int getValue(){
return this.value;
}
public int calcDifference(Rank rank){
return getValue() - rank.getValue();
}
};
可以这样调用:
排名rankAce = Rank.ACE;
System.out.println(rankAce.calcDifference(Rank.TWO));
您可以删除int值,只需使用序数,但这样可以让您有一定的灵活性。
I wish to write a method that can return the difference in value of 2 cards. Im confused as I'm learning enums and not sure of the most efficient way to implement it.
public class Card implements Comparable<Card> {
public enum Rank {TWO, THREE, FOUR, FIVE, SIX,SEVEN, EIGHT, NINE, TEN,
JACK, QUEEN, KING, ACE}
public enum Suit {CLUBS, DIAMONDS, HEARTS, SPADES}
public static int difference(Card c){
}
Any help or guidance would be appreciated.
解决方案
public enum Rank {ACE(1), TWO(2); /* obviously add the other values*/
private int value ;
Rank(int value){
this.value = value;
};
public int getValue(){
return this.value;
}
public int calcDifference(Rank rank){
return getValue() - rank.getValue();
}
};
which can then be called like so :
Rank rankAce = Rank.ACE;
System.out.println(rankAce.calcDifference(Rank.TWO));
You could remove the int value and just use ordinal, but this way gives you a bit of flexibility.
这篇关于差异法java使用枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文