samedi 9 janvier 2021

Java reflection problem: When I have multiple subclasses that inherit a parent class, reflection cannot retrieve some properties from the parent class

Class<?> cls   = bean.getClass();
Field    field = cls.getField(fieldName);
if (field == null) {
return null;
}[enter image description here][1]
field.setAccessible(true);
return (Integer) field.get(bean);

For example, if a and b, c and d inherit from F, F has public String name = 'sun', and when I get it, only a and b get this name, c and d get null. Why? I'm sure I inherited all the F

My current solution is to add static before name, but I don't know the principle, so I hope there is a solution

enter image description here

enter image description here





Aucun commentaire:

Enregistrer un commentaire