Is it possible to use Java Reflection to print out attributes of the parent class?

橙三吉。 提交于 2019-11-26 21:54:23

问题


is it possible to use Java Reflection to print out the attributes of a parent class.


回答1:


Yes, you could do something like this:

Class<?> parentClass = getClass().getSuperclass();

Field[] fields = parentClass.getDeclaredFields();
for (Field field : fields) {
    System.out.println("field: " + field.getName());
}

Method[] methods = parentClass.getDeclaredMethods();
for (Method method : methods) {
    System.out.println("method: " + method.getName());
}



回答2:


Given an appropriately permissive security policy, it is possible to print out any class/instance's attributes using reflection. See How to limit setAccessible to only "legitimate" uses? for some interesting discussion.



来源:https://stackoverflow.com/questions/6099128/is-it-possible-to-use-java-reflection-to-print-out-attributes-of-the-parent-clas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!