学习知识:枚举类型、相关的使用方法
Main.java:
public class Main {public static void main(String[] args) {myenum[] colorlist = myenum.values();//获取枚举中所有对象的引用数组for (myenum one : colorlist){System.out.println(one.toString());}}
}
myenum.java:
public enum myenum {RED(0,"红色"),BLUE(1,"蓝色"),YELLOW(2,"黄色"),PINK(3,"粉色");//可理解为定义四个类//下面的代码为这四个类创建相同的成员变量和成员方法private int index;private String color;//构造方法myenum(int index, String color) {this.index=index;this.color=color;}//对类的toString进行重写public String toString(){return this.index + "_"+this.color;}
}
输出: