import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Member;
import static java.lang.System.out;
public class TryReflection {
public TryReflection() {
int a = 0;
}
public static int getMax() {
int max = 0;
return max;
}
public static void main(String[] args) {
TryReflection myObject = new TryReflection();
int ret = myObject.getMax();
System.out.printf("max is %d\n", ret);
Method[] methods = myObject.class.getMethods();
// for(Method method:methods) {
// System.out.println("method = " + method.getName());
// }
}
}
我不明白为什么在编译上面的代码时会出现以下错误。
TryReflection.java:31: error: cannot find symbol
Method[] methods = myObject.class.getMethods();
^
symbol: class myObject
location: class TryReflection
1 error
最佳答案
myObject
是类的实例,因此您应该使用myObject.getClass()
。或者,只需调用 TryReflection.class
。
关于java - 使用反射检索对象的类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25464700/