自定义的枚举类,编译器会自动集成Enum类,Enum类并没有values方法,通过反射可以知道,values是
编译器添加给父类的一个方法
public class Reflection {
public static Set<String> analyze(Class<?> enumClazz) {
System.out.println("分析" + enumClazz + "类");
for (Type genericInterface : enumClazz.getGenericInterfaces()) {
System.out.println(genericInterface);
}
System.out.println("父类:" + enumClazz.getSuperclass());
System.out.println("方法:");
Set<String> methods = new TreeSet<>();
for (Method method : enumClazz.getMethods()) {
methods.add(method.getName());
}
System.out.println(methods);
return methods;
}
public static void main(String[] args) {
analyze(TestEnum.class);
System.out.println("------");
analyze(Enum.class);
}
}
enum TestEnum {A, B}
//输出:
分析class com.bench.app.oa.web.base.test.webservice.TestEnum类
父类:class java.lang.Enum
方法:
[compareTo, equals, getClass, getDeclaringClass, hashCode, name, notify, notifyAll, ordinal, toString, valueOf, values, wait]
------
分析class java.lang.Enum类
java.lang.Comparable<E>
interface java.io.Serializable
父类:class java.lang.Object
方法:
[compareTo, equals, getClass, getDeclaringClass, hashCode, name, notify, notifyAll, ordinal, toString, valueOf, wait]
————————————————
版权声明:本文为CSDN博主「wanting1024」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_31473465/article/details/89574554
通过values方法我们可以将枚举获取到枚举类的数组,通过下标的形式找到我们需要的枚举对象
版权声明:本文为CSDN博主「wanting1024」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_31473465/article/details/89574554