总共有以下知识点:
构造函数的格式,枚举类中的变量的初始化,类中抽象函数的重写,及枚举类中变量的使用;
public enum TestEnum {
TestEnum1("魏金浩") {
@Override
public void show() {
// TODO Auto-generated method stub
System.out.println(this.getName());
}
},
TestEnum2("周菲菲") {
@Override
public void show() {
// TODO Auto-generated method stub
System.out.println(this.getName());
}
},
TestEnum3("刘亚伟") {
@Override
public void show() {
// TODO Auto-generated method stub
System.out.println(this.getName());
}
},
TestEnum4() {
@Override
public void show() {
// TODO Auto-generated method stub
System.out.println(this.getName());
}
};
private String name;
private TestEnum() {
}
private TestEnum(String name) {
this.name = name;
}
public String getName() {
if(name == null) {
return "null";
}
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract void show();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//事实证明他存的是对象的引用
TestEnum test = TestEnum.TestEnum1;
test.show();
TestEnum test1 = TestEnum.TestEnum2;
test1.show();
TestEnum test2 = TestEnum.TestEnum3;
test2.show();
TestEnum test3 = TestEnum.TestEnum4;
test3.show();
}
我们查看输出的结果: