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

大兔子大兔子 提交于 2019-11-28 01:24:04

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());
}
Dilum Ranatunga

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.

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