如何从 Java 中的子类构造函数中删除参数?
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中的constructor is undefined。我希望子类没有“东西”,但我希望我的超类拥有它。有什么办法吗?
我会将默认值传递给超级 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;
}
然后 sub-class 可以使用适合其上下文的构造函数。
在我的超类中,我定义了参数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中的constructor is undefined。我希望子类没有“东西”,但我希望我的超类拥有它。有什么办法吗?
我会将默认值传递给超级 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;
}
然后 sub-class 可以使用适合其上下文的构造函数。