enum Person{
张三("脾气暴躁"),关公("讲义气"),刘备("爱哭");
private String feature;
private Person(String feature)
{
this.feature = feature;
}
public String getFeature(){
return this.reature;
}
}
怎样理解上述代码:
1,枚举类是一种特殊形式的JAVA 类
2,张三是Person静态 的实例对象
3,("脾气暴躁") 是Person 的私有构造参数值
所以测试方法可以这样写:
@Test
public void test(){
System.out.println(Person.张三.getFeature());
}
因此java的单例也可以用枚举来实现。