class A {
private TypeA a;
Private TypeB b;
...
Private TypeZ z;
...getters/setters...
public add(Object o) {
//blablabla
}
public testMethod() {
add(a);
add(b);
add(c);
......
add(z);
/** here instead of calling add(a), add(b), add(c) one by one, I want to use reflection.
** something like:
** foreach(Field f : getDeclaredFields()) {
** add(f.getTheObjectReference()); <-- I made this method "getTheObjectReference" up
** }
**/
}
}
所以在这个例子中,我可以使用getDeclaredFields获取所有字段Field [az],但是一旦我有了Field对象,我该如何将其转换为实际的对象引用? Field类中没有称为“getTheObjectReference”的方法。有任何想法吗?如何通过使用反射的方法传递类字段