java复制构造函数和继承
经过一番搜索,我没有找到有关复制构造函数和继承的任何好答案。
我有两个课程:User和Trainee。受训者从User继承,并且两个String参数被添加到受训者。
现在我设法制作了User的副本构造函数,但对Trainee的副本构造函数不满意。
用户复制构造函数的代码如下:
After some searching I didn't found any good answer to my question regarding copy constructor and inheritance. I have two classes: User and Trainee. Trainee inherits from User and two String parameters are added to Trainee. Now I managed to make a copy constructor of User but I am not satisfied with my copy constructor of Trainee. The code of the User copy constructor is like this:
public User (User clone) {
this(clone.getId(),
clone.getCivilite(),
clone.getNom(),
clone.getPrenom(),
clone.getEmail(),
clone.getLogin(),
clone.getTel(),
clone.getPortable(),
clone.getInscription(),
clone.getPw()
);
}
我尝试在实习生副本构造函数中使用super:
I tried to use super in my Trainee copy constructor:
public Trainee (Trainee clone) {
super (clone);
this (clone.getOsia(), clone.getDateNaiss());
}
但是它没有用,我被迫编写完整版本的复制构造函数:
But it didn't work and I was forced to code a full version of the copy constructor:
public Trainee (Trainee clone) {
this(clone.getId(),
clone.getCivilite(),
clone.getNom(),
clone.getPrenom(),
clone.getEmail(),
clone.getLogin(),
clone.getTel(),
clone.getPortable(),
clone.getInscription(),
clone.getPw(),
clone.getOsia(),
clone.getDateNaiss()
);
}
由于主体的构造,我必须像这样铸造新实例:
Because of the construction of my main I have to cast my new instance like this:
User train = new Trainee();
User train2 = new Trainee((Trainee) train);
所以我的问题是:有没有更清洁的方法?我不能使用超级吗?
So my question is: Is there a cleaner way to do this? Can't I use a super?
在此先感谢您的回答和帮助。
Thank you in advance for your answers and assistance.
最好将学员
的完整副本构造函数也包含一个 User
:
It would be better to make the "full" copy constructor for Trainee
also take a User
:
public Trainee(Trainee clone)
{
this(clone, clone.getOsai(), clone.getDateNaiss());
}
public Trainee(User clone, String osai, String dateNaiss)
{
super(clone);
this.osai = osai;
this.dateNaiss;
}
尽可能地,拥有一个大师模式是值得的每个类中的构造函数,所有其他构造函数都直接或间接链接到该类。
As far as possible, it's worth keeping to the pattern of having one "master" constructor in each class, which all other constructors chain to, directly or indirectly.
现在,尚不清楚创建培训生 没有指定现有的用户信息...或可能以其他方式指定它。可能是 ,在这种情况下,您确实需要两个独立的构造函数集-一组用于复制构造函数,而另一组用于仅给我所有值个别的构造函数。确实取决于您的上下文-我们不能仅凭此判断。
Now, it isn't clear whether it makes sense to create a Trainee
without specifying existing user information... or possibly specifying it in some other way. It may be that in this case you really do need to have two separate sets of constructors - one set for copy constructors, and one set for "just give me all the values individually" constructors. It really depends on your context - we can't tell just from this.
在那种情况下,您可能会稍微违反一个主构造函数规则,但是您可以想到有两个 主构造函数,每个构造函数分别用于不同目的。从根本上讲,您遇到了继承变得混乱的情况-这很常见:(
In that case, you would be breaking the "one master constructor" rule slightly, but you can think of there being two master constructors, one for each set of constructors for different purposes. Fundamentally, you're running into "inheritance gets messy" - which is all too common :(