java 复制构造_java复制构造函数和继承

经过一番搜索,我没有找到有关复制构造函数和继承的任何好答案.

我有两个班:用户和学员.受训者从User继承,并且两个String参数被添加到受训者.

现在,我设法制作了User的副本构造函数,但对Trainee的副本构造函数不满意.

用户复制构造函数的代码如下:

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()

);

}

我尝试在我的Trainee复制构造函数中使用super:

public Trainee (Trainee clone) {

super (clone);

this (clone.getOsia(), clone.getDateNaiss());

}

但这没有用,我被迫编写完整版本的复制构造函数的代码:

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()

);

}

由于主体结构的原因,我必须像这样强制转换新实例:

User train = new Trainee();

User train2 = new Trainee((Trainee) train);

所以我的问题是:是否有更清洁的方法来做到这一点?我不能使用超级吗?

预先感谢您的答复和协助.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值