![](/img/trans.png)
[英]How can I insert code from subclass constructor in superclass constructor?
[英]How can I remove a parameter from a subclass constructor in Java?
在我的超类中,我定义了参数 a、b、c 和 d。 对于它的子类,我想删除 d。 我有什么办法可以做到这一点吗?
abstract class Prescription {
protected Medicine Medicine;
protected Doctor doctor;
protected int patientID, thing;
public Resept(Medicine medicine, Doctor doctor, int patientID, int thing) {
this.legemiddel = legemiddel;
this.lege = utskrivendeLege;
this.pasientID = pasientID;
this.thing = thing;
在我的子类中,我想创建一个没有最后一个参数“thing”的构造函数
public class TypeBPrescription extends Prescription {
public TypeBPresciption(Medicine medicine, Doctor doctor, int patientID){
super(Medicine medicine, Doctor doctor, int patientID,)
}
}
像这样写给我的错误是子类 TypeBPrescription 中的构造函数未定义。 我希望子类没有“东西”,但我希望我的超类拥有它。 有什么办法吗?
我会将默认值传递给超级 class。
public TypeBPresciption(Medicine medicine, Doctor doctor, int patientID){
super(medicine, doctor, patientID, 0)
}
向 super 添加多个构造函数以澄清thing
是可选的:
abstract class Prescription {
private static final int DEFAULT_THING = 0;
protected Medicine Medicine;
protected Doctor doctor;
protected int patientID, thing;
public Prescription (Medicine medicine, Doctor doctor, int patientID)
{
this(medicine, doctor, paitentId, DEFAULT_THING);
public Prescription (Medicine medicine, Doctor doctor, int patientID, int thing) {
this.legemiddel = legemiddel;
this.lege = utskrivendeLege;
this.pasientID = pasientID;
this.thing = thing;
}
然后子类可以使用适合其上下文的构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.