1、方式一、自定义一个枚举类
public enum Birds {
WU_YA(1, "小乌鸦", 3),
ZHUO_MU_MIAO(2, "啄木鸟", 2),
XI_QUE(3, "喜鹊", 5);
private Integer id;
private String name;
private Integer age;
Birds(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public Integer getHeight() {
return age;
}
}
2、方式二、自定一个接口,然后接口中定义枚举类型
public interface Organism {
/**
* 自定义 Animal 枚举类
*/
enum Animal{
ELEPHANT(11,"大象",6000),
HIPPO(22,"河马",3000),
LION(33,"狮子",400);
private Integer id;
private String name;
private Integer weight;
Animal(Integer id,String name,Integer weight){
this.id=id;
this.name=name;
this.weight=weight;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public Integer getWeight() {
return weight;
}
}
/**
* 自定义 Plant 枚举类
*/
enum Plant{
LOTUS(111,"荷花","红色"),
PENOY(222,"牡丹","白色"),
ROSE(333,"玫瑰","蓝色");
Plant(Integer id,String name,String color){
this.id=id;
this.name=name;
this.color = color;
}
private Integer id;
private String name;
private String color;
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public String getColor() {
return color;
}
}
}
3、测试
public class TestEnums {
public static void main(String[] args) {
System.out.println(Birds.WU_YA.getId()+"---"+Birds.WU_YA.getName()+"---"+Birds.WU_YA.getHeight());
System.out.println(Birds.XI_QUE.getId()+"---"+Birds.XI_QUE.getName()+"---"+Birds.XI_QUE.getHeight());
System.out.println(Birds.ZHUO_MU_MIAO.getId()+"---"+Birds.ZHUO_MU_MIAO.getName()+"---"+Birds.ZHUO_MU_MIAO.getHeight());
System.out.println(Organism.Animal.ELEPHANT.getId()+"---"+Organism.Animal.ELEPHANT.getName()+"---"+Organism.Animal.ELEPHANT.getWeight());
System.out.println(Organism.Animal.HIPPO.getId()+"---"+Organism.Animal.HIPPO.getName()+"---"+Organism.Animal.HIPPO.getWeight());
System.out.println(Organism.Animal.LION.getId()+"---"+Organism.Animal.LION.getName()+"---"+Organism.Animal.LION.getWeight());
System.out.println(Organism.Plant.LOTUS.getId()+"---"+Organism.Plant.LOTUS.getName()+"---"+Organism.Plant.LOTUS.getColor());
System.out.println(Organism.Plant.PENOY.getId()+"---"+Organism.Plant.PENOY.getName()+"---"+Organism.Plant.PENOY.getColor());
System.out.println(Organism.Plant.ROSE.getId()+"---"+Organism.Plant.ROSE.getName()+"---"+Organism.Plant.ROSE.getColor());
}
}
4、测试结果