首页 > 代码库 > Java中的枚举
Java中的枚举
public class Su{/*随便新建一个类*/
enum Person{
Big("bigP",1),Samll("Samll",2);
private int index;
private String name;
private Person(String name,int index){
this.name = name;
this.index = index;
}
public static String getName(int index){
for (Person c : Person.values()) {
if (c.getIndex() == index) {
return c.name;
}
}
return null;
}
public void setIndex(int index){
this.index = index;
}
public int getIndex(){
return index;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void switchTest(){
switch(this){
case Big:{
System.out.println("大人");
}
break;
case Samll:{
System.out.println("小人");
}
break;
}
}
}
public static void testEnum(){
for (Person p :Person.values()){
System.out.println(p);
System.out.println(p.getName());
p.switchTest();
}
}
public static void main(String[] args){
Su.testEnum();
}
}
Java中的枚举