[英]How can I remove a parameter from a subclass constructor in Java?
In my superclass, I have defined the parameters a, b, c and d.在我的超类中,我定义了参数 a、b、c 和 d。 For its subclass, I want to remove d.
对于它的子类,我想删除 d。 Is there any way for me to do this?
我有什么办法可以做到这一点吗?
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;
And in my subclass, I would like to create a constructor without the last parameter "thing"在我的子类中,我想创建一个没有最后一个参数“thing”的构造函数
public class TypeBPrescription extends Prescription {
public TypeBPresciption(Medicine medicine, Doctor doctor, int patientID){
super(Medicine medicine, Doctor doctor, int patientID,)
}
}
Writing it like this gives me the error that the constructor in the subclass TypeBPrescription is undefined.像这样写给我的错误是子类 TypeBPrescription 中的构造函数未定义。 I want the subclass to not have "thing", but I want my superclass to have it.
我希望子类没有“东西”,但我希望我的超类拥有它。 Is there any way to do this?
有什么办法吗?
I would pass a default value to the super class.我会将默认值传递给超级 class。
public TypeBPresciption(Medicine medicine, Doctor doctor, int patientID){
super(medicine, doctor, patientID, 0)
}
Add multiple constructors to the super to clarify thing
is optional:向 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;
}
Then a sub-class can use whichever constructor fits their context.然后子类可以使用适合其上下文的构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.