简体   繁体   English

如何从 Java 中的子类构造函数中删除参数?

[英]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.

相关问题 如何在超类构造函数中从子类构造函数插入代码? - How can I insert code from subclass constructor in superclass constructor? Java-如何为子类的子类调用复制构造函数? - Java - how do I call copy constructor for subclass of subclass? 当我需要使用带有参数的构造函数时,如何使用Rhino子类化(扩展)Java类? - How can I subclass (extend) a Java class using Rhino when I need to use a constructor that takes an argument? 子类类型作为超类构造函数java中的参数类型 - A subclass type as parameter type in superclass constructor java 将超类对象作为参数传递给子类构造函数(java) - Passing superclass object as parameter to subclass constructor (java) 来自超类的Java子类构造函数 - Java subclass constructor from superclass 从Java中的子类构造函数调用超级构造函数 - Calling super constructor from subclass constructor in Java 如何在java中声明子类构造函数 - How to declare subclass constructor in java 当baseclass有一个带参数的构造函数时,为什么我不能创建一个无参数的子类构造函数? - Why can't I create a parameterless subclass constructor when the baseclass has a constructor with a parameter? 我可以覆盖使用子类对象作为Java中的参数吗? - Can I override using subclass object as parameter in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM