Java枚举,整数和字符串一起定义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18248977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java enum, integer and string together define?
提问by CursedChico
I want to define string and integer together but it gives errors.
我想一起定义字符串和整数,但它给出了错误。
public class Card {
Rank rank;
Suit suit;
public Card(Rank rank, Suit suit){
this.rank=rank;
this.suit=suit;
}
public enum Rank { Ace, 9, Queen, King } //and other suits
}
The error is a syntax error on token 9, delete this token.
错误是标记 9 上的语法错误,请删除此标记。
回答by Duncan Jones
You cannot start an enum field with a number. Try using NINE
.
您不能以数字开头枚举字段。尝试使用NINE
.
回答by Tala
In declaring enum in Java { Ace, 9, Queen, King }
these are not strings and integers. These are actual objects of enum.
在 Java 中声明枚举时,{ Ace, 9, Queen, King }
这些不是字符串和整数。这些是枚举的实际对象。
You can do this:
你可以这样做:
public enum Rank {
Ace(13),
Nine(8),
//etc
;
private int rank;
Rank(int rank) {
this.rank = rank;
}
public int getRank() {
return rank;
}
}
回答by ug_
Java naming rules say you cannot start variables, class names ect... with numbers.
Java命名规则说你不能用数字开始变量、类名等等。
回答by No Idea For Name
try
尝试
public enum Rank { Ace, Hyman, Queen, King }//and other suits
instead of your enum
而不是你的枚举
the problem is that if you could have put number as enum value then the compiler won't know, when someone write "9" it he meant the number or the enum. so java, as almost every language, won't allow this kind of enum value
问题是,如果您可以将数字作为枚举值,那么编译器将不知道,当有人写“9”时,他的意思是数字或枚举。所以java,几乎所有的语言,都不允许这种枚举值
回答by Jazib
U cannot use integers inside enum like this. Please go through this: http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
你不能像这样在枚举中使用整数。请通过这个:http: //docs.oracle.com/javase/tutorial/java/javaOO/enum.html
回答by S.D.
The things inside enum
s are identifiers, names of static final (constant) objects that'll be created. So, you can't use an int for naming an object.
enum
s里面的东西是标识符,将被创建的静态最终(常量)对象的名称。因此,您不能使用 int 来命名对象。
enum
s allow you to place fields for each entry:
enum
s 允许您为每个条目放置字段:
public static enum Suit {HEART,DIAMOND,SPADE,CLUB}
public static enum Cards {
ACE_OF_HEART(Suit.HEART,14), QUEEN_OF_SPADE(Suit.SPADE,13) /* etc etc*/;
private final Suit mSuit;
private final int mRank;
private Cards(Suit suit, int rank) {
assert rank >= 2 && rank <= 14;
mSuit = suit;
mRank = rank;
}
public Suit getSuit() {
return mSuit;
}
public int getRank() {
return mRank;
}
}
You really don't want to code all 52 cards this way. You can model it another way:
您真的不想以这种方式对所有 52 张卡片进行编码。您可以以另一种方式对其进行建模:
Suite:
套房:
public static enum Suit { SPADE, HEART, DIAMOND, CLUB};
Class with some popular ranks as named constants:
将一些流行等级作为命名常量的类:
public class Card{
public static final int ACE = 14;
public static final int QUEEN = 13;
public static final int KING = 12;
public static final int Hyman = 11;
private final int mRank;
private final Suite mSuit;
public Card(Suite s, int r){
this.mSuit = s;
if(r < 2 || r > 14){
throw new IllegalArgumentException("No such card with rank: "+r);
}
this.mRank = r;
}
public Suit getSuit() {
return mSuit;
}
public int getRank() {
return mRank;
}
}