enum
在Java和Android开发中都较为常用,比如下面的举例:
public enum Week {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
编写测试类
public class TestEnum {
public static void main(String[] args) {
dayOfWeek(Week.MONDAY);
}
private static void dayOfWeek(Week week) {
switch (week) {
case MONDAY:
System.out.println("Today is MONDAY!");
break;
case THURSDAY:
System.out.println("Today is THURSDAY!");
default:
break;
}
}
}
打印输出
Today is MONDAY!
接下来演示一遍带构造器的enum,直接上代码吧。
enum Foo {
NEWS(0),
TWEET(1),
QUICK(2),
EXPLORE(3),
ME(4);
private int i;
private Foo(int i) {
this.i = i;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
}
编写测试类
public class TestEnum {
public static void main(String[] args) {
for (Foo foo : Foo.values()) {
System.out.println(foo.getI());
}
}
}
打印输出
0 1 2 3 4