我有以下两类:拷贝构造函数的孩子,但没有家长
public class User {
public Integer userId;
// ...another 50-60 fields
}
public class SuperUser extends User {
}
我想有超级用户构造这需要用户类型的对象,并创建类型为超级用户的对象。例如:
public SuperUser(User theUser) {
// not legal -> but I am looking for a one-liner to initialize this with values from theUser
this = theUser;
}
如果用户对象缺少构造用户(用户existingUser),是否有任何自动的方式来初始化与所有来自现有用户对象的字段的超级用户对象?我试图避免50行:
public SuperUser(User theUser) {
this.firstName = theUser.getFirstName();
this.lastName = theUser.getLastName();
// and so on....
}
如果不能这样做,是否有像“创建复制构造函数?”的重构。
谢谢!
我不认为你所回答的问题;) – Bozho 2010-06-22 17:13:10
但是,如果我没有记错,在Java中没有真正的专用拷贝构造函数,那么如何使用它们? – AndrejaKo 2010-06-22 17:17:48
您可以手动实施它们。 – royal 2010-06-22 23:29:49