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”的方法。有任何想法吗?