说我有一个具有一些成员变量的类,一个或多个是具有成员变量的另一个类。如何获得两个类中每个变量的值?
我可以通过以下方式获取变量:通过调用getDeclaredFields();Foo对象的值,field.get(obj)然后再次对其进行迭代以获取子类变量。
但是,如何在没有该类实例的情况下获取每个子类成员变量的值?我的平凡示例之所以有用,是因为Bar该类的每个实例的变量都相同,因此我可以调用innerField.get(new
Bar())
我想如果能以某种方式从字段中获取对象的实例,那可能会起作用?
提前致谢
第一个对象:
public class Foo {
public Foo(){}
private Integer someInt = 123;
private Long someLong = 987654321L;
private String someString = "qwertyuiop";
private Bar bar = new Bar();
}
第二个对象:
public class Bar {
public Bar(){}
private Double someDouble = 123.456;
private String innerString = "another String";
}
主/调用方法:
public static void main(String[] args) throws Exceptio