我正在使用此代码获取与对象关联的所有字段。所讨论的对象具有三个父类(super class),其他类从该对象扩展而来。
for (Field f : this.getClass().getFields()){
try{
fieldName = f.getName();
fieldType = f.getType().toString();
//Do more stuf
}
我不想获取与父类(super class)关联的字段,但我想要当前类中的所有字段以及所有可能从它扩展的字段。
如何筛选出我需要的字段?
最佳答案
I do not want to get the fields associated with the super classes, but I want all fields in the current class and all those that may extend from it.
你不能得到“所有可能从它延伸出来的”——但如果你使用 Class.getDeclaredFields()
而不是 getFields()
那么您将只会获得在该类中声明的字段。
关于Java - 从反射中排除父类(super class)字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13464288/