与枚举在安卓工作 [英] Working with Enums in android
问题描述
我快与计算活动我正与Android中为我的应用程序进行。我尝试创建一个性别枚举,但出于某种原因,获得的语法错误,将EnumBody完成EnumDeclaration。
I am almost done with an calculation activity I am working with in android for my app. I am try to create a Gender Enum, but For some reason getting Syntax error, insert "EnumBody" to complete EnumDeclaration.
public static enum Gender
{
static
{
Female = new Gender("Female", 1);
Gender[] arrayOfGender = new Gender[2];
arrayOfGender[0] = Male;
arrayOfGender[1] = Female;
ENUM$VALUES = arrayOfGender;
}
}
我也试了一下没有静态{}但我得到了相同的语法错误。我一直在Google上搜寻一整天阅读所有关于枚举,我在编程绝对新手我刚开始上大学的软件工程,如果这是一个愚蠢的问题,时间我道歉提前。
I have also tried it without the static {} but I get the same syntax error. I have been googling all day reading all about enums, I am an absolute beginner in programming I just start started college for software engineering if this is a stupid question, I apologize ahead of time.
推荐答案
到底从哪里找到这样的语法? Java的枚举很简单,你只需指定的值。
Where on earth did you find this syntax? Java Enums are very simple, you just specify the values.
public enum Gender {
MALE,
FEMALE
}
如果你想他们是更复杂的,你可以像这样添加值给他们。
If you want them to be more complex, you can add values to them like this.
public enum Gender {
MALE("Male", 0),
FEMALE("Female", 1);
private String stringValue;
private int intValue;
private Gender(String toString, int value) {
stringValue = toString;
intValue = value;
}
@Override
public String toString() {
return stringValue;
}
}
然后用枚举,你会做这样的事情:
Then to use the enum, you would do something like this:
Gender me = Gender.MALE
这篇关于与枚举在安卓工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!