public enum Colar {
RED("紅色", 1), GREEN("綠色", 2), BLANK("白色", 3), YELLO("黃色", 4);
? ? private Stringname ;
? ? private int index ;
? ? private Colar( String name, int index ){
????????this.name = name;
? ? ? ? this.index = index;
? ? }
public String getName() {
return name;
? ? }
public void setName(String name) {
this.name = name;
? ? }
public int getIndex() {
return index;
? ? }
public void setIndex(int index) {
this.index = index;
? ? }
}
class B {
public static void main(String[] args) {
//輸出某一枚舉的值
? ? ? ? System.out.println( Colar.RED.getName() );
? ? ? ? System.out.println( Colar.RED.getIndex() );
? ? ? ? //遍歷所有的枚舉
? ? ? ? for( Colar color : Colar.values()){
System.out.println( color +"? name: " + color.getName() +"? index: " + color.getIndex() );
? ? ? ? }
}
}
個人公號:【排骨肉段】,可以關(guān)注一下。