public class Test {
public void test() {
String currentType = ConnectionType.Zondy.getType();
System.out.println("ConnectionType = "+currentType);
}
public enum ConnectionType {
/*These are public static final field*/
Zondy("Zondy"),
Shjw("Shjw"),
Tn("Tn");
/*上述的定义,相当于:
* public enum ConnectionType {
* Zondy,Shjw,Tn
* }
*
* 大致等同于:
* public class ConnectionType {
* public static final int Zondy = 0;
* public static final int Shjw = 1;
* public static final int Tn =2;
* }
*
* 如果需要给枚举的实例添加附加信息,需要先列出所有的枚举实例,并以‘;’表示结束,然后定义private 构造函数,没错,必须是private的构造函数,因为
* 是在枚举类内部调用,此外用户也可以定义public属性的通用接口。*/
private String type = null;
/*Constructor --- must be private*/
private ConnectionType(String type) {
this.type = type;
}
/*Normal methods*/
public String getType() {
return type;
}
}
}