枚举类的基本用法
程序员文章站
2022-06-16 14:36:13
...
枚举类代码:
public enum Color {
RED("red",10){
@Override
public String getColor() {
return "红色";
}
},
YELLOW("yellow",20){
@Override
public String getColor() {
return "黄色";
}
},
BLUE("blue",30){
@Override
public String getColor() {
return "蓝色";
}
};
public String name;
public int number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
private Color(String name,int number){
this.name = name;
this.number = number;
}
public abstract String getColor();
}
测试类代码:
public class Test {
public static void main(String[] args) {
System.out.println(Color.RED.getColor());
System.out.println(Color.RED.name);
System.out.println(Color.RED.number);
}
}
运行后的结果: 红色 red 10
转载于:https://my.oschina.net/u/3441184/blog/885543