如何从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;

在我的子类中,我想创建一个没有最后一个参数"的构造函数;事物;

public class TypeBPrescription extends Prescription {
public TypeBPresciption(Medicine medicine, Doctor doctor, int patientID){
super(Medicine medicine, Doctor doctor, int patientID,)
}
}

这样写会给我一个错误,即子类TypeBPrescription中的构造函数是未定义的。我希望子类不具有";事物";,但我想让我的超类拥有它。有办法做到这一点吗?

我会将一个默认值传递给超级类。

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;
}

然后,子类可以使用任何适合其上下文的构造函数。

相关内容

最新更新