java的枚举

时间:2022-04-21 09:08:55
package com.wzh.test.enum0;

import org.junit.Test;

public class demo1 {

	@Test
public void testAb(){
System.out.println(Ab.a.getValue());
} //如果定义枚举的构造函数,方法和字段,去封装更多的信息
enum Ab{
a("100-90"),
b("89-80"),
c("79-70"); private String value;
private Ab(String value){
this.value=value;
} public String getValue() {
return this.value;
}
}
}

  

package com.wzh.test.enum1;

import org.junit.Test;

public class demo1 {

	@Test
public void test2(){
System.out.println(Ab.c.name());
System.out.println(Ab.a.ordinal()); String str="b";
Ab a=Ab.valueOf(str);
System.out.println(a); Ab arr[]= Ab.values();
for(Ab ab : arr){
System.out.println(ab);
}
} @Test
public void testAb() {
System.out.println(Ab.b.localValue()); System.out.println(Ab.values());
} // 带抽象方法的枚举
enum Ab {
a("100-90") {
public String localValue() {
return "优";
}
},
b("89-80") {
public String localValue() {
return "良";
}
},
c("79-70") {
public String localValue() {
return "及格";
}
}; private String value; private Ab(String value) {
this.value = value;
} public String getValue() {
return this.value;
} public abstract String localValue(); }
}