Java5中Enum的特点:
1.Enum是class;
2.Enum是final的,不可被继承的,值也是不可覆盖的;
3.Enum可以使用"=="和"equals()"来进行比较;
在Java5之前的Enum:
1 package com.kay.java5.test1;
2
3 public final class Grade {
4
5 public static final String A = "a";
6 public static final String B = "b";
7 public static final String C = "c";
8 public static final String D = "d";
9 public static final String E = "e";
10 }
2
3 public final class Grade {
4
5 public static final String A = "a";
6 public static final String B = "b";
7 public static final String C = "c";
8 public static final String D = "d";
9 public static final String E = "e";
10 }
Java5中的Enum:
1 package com.kay.java5.test1;
2
3 public enum GradeEnum {
4 a,b,c,d,e,f
5 }
6
2
3 public enum GradeEnum {
4 a,b,c,d,e,f
5 }
6
或者是这样的:
1 package com.kay.java5.test1;
2
3 public class EnumTest {
4
5 private enum grade{A,B,C,D};
6
7 private static enum grade2{F,G,H};
8 }
2
3 public class EnumTest {
4
5 private enum grade{A,B,C,D};
6
7 private static enum grade2{F,G,H};
8 }
打印Enum中的内容:
package com.kay.java5.test1;
public class Test {
public static void main(String[] args) {
GradeEnum[] ge = GradeEnum.values();//values()方法返回Enum中所有值的Array
for (int i = 0; i < ge.length; i++) {
System.out.println(ge[i]);
}
}
}
public class Test {
public static void main(String[] args) {
GradeEnum[] ge = GradeEnum.values();//values()方法返回Enum中所有值的Array
for (int i = 0; i < ge.length; i++) {
System.out.println(ge[i]);
}
}
}
switch中的Enum:
package com.kay.java5.test1;
public class Test {
private static enum e {A,B,c,D};
public static void main(String[] args) {
switch(e.A){
case A:
System.out.println("A");
break;
case B:
System.out.println("B");
default:
System.out.println("C or D");
}
}
}
public class Test {
private static enum e {A,B,c,D};
public static void main(String[] args) {
switch(e.A){
case A:
System.out.println("A");
break;
case B:
System.out.println("B");
default:
System.out.println("C or D");
}
}
}
EnumMap是Java5中使用Enme作为Map中Key的一种新集合:
package com.kay.java5.test1;
import java.util.EnumMap;
public class Test {
public static void main(String[] args) {
//EnumMap构造方法<Enum,Object>
EnumMap<GradeEnum,String> emap = new EnumMap<GradeEnum,String>(GradeEnum.class);
//赋值
emap.put(GradeEnum.a, "This is A!");
emap.put(GradeEnum.b, "This is b!");
emap.put(GradeEnum.c, "This is c!");
emap.put(GradeEnum.d, "This is d!");
emap.put(GradeEnum.e, "This is e!");
emap.put(GradeEnum.f, "This is f!");
//取值
System.out.println(emap.get(GradeEnum.a));
}
}
import java.util.EnumMap;
public class Test {
public static void main(String[] args) {
//EnumMap构造方法<Enum,Object>
EnumMap<GradeEnum,String> emap = new EnumMap<GradeEnum,String>(GradeEnum.class);
//赋值
emap.put(GradeEnum.a, "This is A!");
emap.put(GradeEnum.b, "This is b!");
emap.put(GradeEnum.c, "This is c!");
emap.put(GradeEnum.d, "This is d!");
emap.put(GradeEnum.e, "This is e!");
emap.put(GradeEnum.f, "This is f!");
//取值
System.out.println(emap.get(GradeEnum.a));
}
}
取出EnumMap中全部的值:
GradeEnum[] ge = GradeEnum.values();
for (int i = 0; i < ge.length; i++) {
System.out.println(emap.get(ge[i]));
}
for (int i = 0; i < ge.length; i++) {
System.out.println(emap.get(ge[i]));
}
EnumSet:
EnumSet es = EnumSet.allOf(GradeEnum.class);//将GradeEnum中所有元素放入EnumSet
EnumSet ee = EnumSet.of(GradeEnum.a,GradeEnum.b);//将GradeEnum中指定元素放入EnumSet
//取值
Iterator iter = ee.iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}
EnumSet ee = EnumSet.of(GradeEnum.a,GradeEnum.b);//将GradeEnum中指定元素放入EnumSet
//取值
Iterator iter = ee.iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}