java 判断返回值为枚举类型

2024-11-15 19:28:28
推荐回答(3个)
回答1:

public class Test {

public enum ColorSelect {
red, green, yellow, blue;
}
private static ColorSelect getE()
{
ColorSelect c=ColorSelect.blue;
return c;
}
public static void main(String[] args) {
Object o=getE();
if(o instanceof Enum)
{
System.out.println(o);
}
}
}

回答2:

import java.lang.reflect.Method;

public class Test {
enum Grade {
A, B, C, D, F
};

public static void main(String args[]) throws Exception {
Test ref = new Test();
ref.getConstructor();

}

public String test1() {
return "";
}

public Grade test2() {
return null;
}

public void getConstructor() throws Exception {
Class c = null;
c = Class.forName("Test");

Method m[] = c.getMethods();
System.out.println("1、返回类所有的公共衡好埋成员方法:");
for (int i = 0; i < m.length; i++) {
System.out.println(m[i].toString());
}
Method m1 = c.getMethod("test1", new Class[] {});
Method m2 = c.getMethod("test2", new Class[] {});
boolean flag1 = m1.getReturnType().isEnum();
boolean flag2 = m2.getReturnType().isEnum();
System.out.println("2、返回指定公共成员方法:");
System.out.println(m1.toString() + "是枚举袜激?" + flag1);
System.out.println(m2.toString() + "是枚举?"咐蚂 + flag2);

}
}

回答3:

你写的反射类呢?
贴代码啊