我试图创建一个域类构造函数,该结构动态地继承另一个类的属性。但是我无法使其正常工作。
这里是一个例子:
class Example1 {
String name;
String location;
}
class Example2 extends Example1 {
String status;
public Example2 (Example1 orig){
// Code here to set this.name and this.location to name and location from orig
// dynamically, so adding a field in Example1 does not require me to add that
// field here.
}
}
最佳答案
您太辛苦了,只需复制属性即可:
class Example2 extends Example1 {
String status
Example2() {}
Example2(Example1 orig) {
this.properties = orig.properties
}
}