一般用于定义一组相类似的常量
2.古老方式定义枚举类public class TestOne { public static void main(String[] args) { System.out.println(People.LIU); System.out.println(People.LI); } } class People { private final String name; private final Integer age; People(String name, Integer age) { this.name = name; this.age = age; } public static final People LIU = new People("liu", 12); public static final People LI = new People("li", 23); @Override public String toString() { return "People{" + "name='" + name + ''' + ", age=" + age + '}'; } }3.Enum关键字定义 3.1 默认覆盖
public class TestOne { public static void main(String[] args) { System.out.println(People.LIU); System.out.println(People.LI); //补充getClass和getDeclaringClass的区别 看getDeclaringClass源码 System.out.println(People.LI.getDeclaringClass()); System.out.println(People.LI.getClass()); } } enum People { LIU("liu",12), LI("li",24); private final String name; private final Integer age; People(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public Integer getAge() { return age; } }
结果图
差异主要看源码,源码如下
public final ClassgetDeclaringClass() { Class> clazz = getClass(); Class> zuper = clazz.getSuperclass(); return (zuper == Enum.class) ? (Class )clazz : (Class )zuper; }
多判定更加稳定
3.2 手动覆盖@Override public String toString() { return "People{" + "name='" + name + ''' + ", age=" + age + '}'; }
二、常用方法 1.values()结果图
public class TestOne { public static void main(String[] args) { System.out.println(People.LIU); System.out.println(People.LI); //补充getClass和getDeclaringClass的区别 看getDeclaringClass源码 System.out.println(People.LI.getDeclaringClass()); System.out.println(People.LI.getClass()); System.out.println("----------------------------------------"); for (People value : People.values()) { System.out.println(value); } } } enum People { LIU("liu",12), LI("li",24); private final String name; private final Integer age; People(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public Integer getAge() { return age; } }
结果图
PS:个人理解,就是通过values()将自定义枚举类变成一个数组
2.valueOf()很简单…就是在括号内输入想要的对象名,得到该对象
System.out.println("---------------------------------------"); People liu = People.valueOf("LIU"); System.out.println(liu);
3.toString()结果图
System.out.println("---------------------------------------");System.out.println(People.LI.toString());
三、接口实现 1.接口实现结果图
public class TestOne { public static void main(String[] args) { People.valueOf("LIU").show(); People.valueOf("LI").show(); People.LIU.show(); People.LI.show(); System.out.println(People.LI.getDeclaringClass()); System.out.println(People.LI.getClass()); } } interface DetailInfo { void show(); } enum People implements DetailInfo{ LIU("liu",12){ @Override public void show() { System.out.println("溜溜溜"); } }, LI("li",24) { @Override public void show() { System.out.println("莉莉力"); } }; private final String name; private final Integer age; People(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public Integer getAge() { return age; } }2.结果图
这里我们就可以发现,上面我提到的getDeclaringClass和getClass的区别了。
参考:https://www.bilibili.com/video/BV1Kb411W75N?p=502
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)