简体   繁体   English

子类的Java构造函数

[英]Java Constructor of a Subclass

I have a subclass extending a superclass. 我有一个扩展超类的子类。 If constructor in super class has parameters a ,b,c like MySuperClass(int a, string b, string c) . 如果超类中的构造函数具有参数a ,b,c,如MySuperClass(int a, string b, string c) And the constructor in the subclass has parameters a ,d,e like MySubClass(int a, int d, int e) , what should go inside the constructor of the subclass? 并且子类中的构造函数具有参数a ,d,e,如MySubClass(int a, int d, int e) ,应该在子类的构造函数内部进行什么? Can I say super(a) so I don't have to duplicate the code for parameter a ? 我可以说super(a)所以我不必复制参数a的代码吗? but the constructor of super has 3 parameters; 但是super的构造函数有3个参数; so I think I cannot do that. 所以我想我做不到。

Also, if I just ignore using super and assign the fields to parameters (like this.fieldName=parameterName ) I would get the error that "there is no default constructor in super" why do I get this even though the super class has a constructor? 另外,如果我只是忽略使用super并将字段分配给参数(比如this.fieldName=parameterName ),我会得到“超级中没有默认构造函数”的错误,为什么我会得到这个,即使超类有一个构造函数?

public abstract class Question {

    // The maximum mark that a user can get for a right answer to this question.
    protected double maxMark;

    // The question string for the question.
    protected String questionString;

    //  REQUIRES: maxMark must be >=0
    //  EFFECTS: constructs a question with given maximum mark and question statement
    public Question(double maxMark, String questionString) {
        assert (maxMark > 0);

        this.maxMark = maxMark;
        this.questionString = questionString;
    }
}

public class MultiplicationQuestion extends Question{

    // constructor
    // REQUIRES: maxMark >= 0
    // EFFECTS: constructs a multiplication question with the given maximum 
    //       mark and the factors of the multiplication.
    public MultiplicationQuestion(double maxMark, int factor1, int factor2){
        super(maxMark);
    }
}

The first thing a constructor always does is to call its superclass' constructor. 构造函数总是做的第一件事是调用它的超类'构造函数。 Omitting the super call doesn't circumvent this - it's just syntactic sugar that saves you the hassle of specifying super() (ie, calling the default constructor) explicitly. 省略super调用并不能避免这种情况 - 它只是语法糖,可以省去显式指定super() (即调用默认构造函数)的麻烦。

What you could do is pass some default value to the superclass' constructor. 你可以做的是将一些默认值传递给超类的构造函数。 Eg: 例如:

public class SubClass {
    private int d;
    private int e;

    public SubClass(int a, int d, int e) {
        super(a, null, null);
        this.d = d;
        this.e = e;
    }
}

If constructor in super class has parameters a,b,c like MySuperClass(int a, string b, string c). 如果超类中的构造函数具有参数a,b,c,如MySuperClass(int a,string b,string c)。 And the constructor in the subclass has parameters a,d,e like MySubClass(int a, int d, int e), what should go inside the constructor of the subclass? 并且子类中的构造函数具有参数a,d,e,如MySubClass(int a,int d,int e),应该在子类的构造函数内部进行什么?

You are the only one to make this decision because it depends on what the numbers mean for your business case. 您是唯一做出此决定的人,因为这取决于数字对您的业务案例的意义。 As long as they are just numbers without any semantic meaning it does not matter. 只要它们只是没有任何语义含义的数字就没关系。

Can I say super(a) so I don't have to duplicate the code for parameter a? 我可以说super(a)所以我不必复制参数a的代码吗?

No, you have to specify which of the classes contructor parameters or constants should be passed to the constructor of the super class. 不,您必须指定应将哪些类的构造函数参数或常量传递给超类的构造函数。 Again there is no "automatic" solution. 再次没有“自动”解决方案。 It is your responsibility as the programmer to decide which values are passed to the super class constructor and where they come from. 作为程序员,您有责任决定将哪些值传递给超类构造函数以及它们来自何处。

why do I get this even though the super class has a constructor? 为什么我会得到这个,即使超类有一个构造函数?

the super classes constructor is not a default constructor (which has no parameters). 超类构造函数不是默认构造函数(没有参数)。

And how can I solve this issue? 我该如何解决这个问题?

Once more this has no general answer. 再一次,这没有一般的答案。 Usually the only valid way is to provide values to pass to the super classes constructor. 通常唯一有效的方法是提供传递给超类构造函数的值。 In very rare cases it might be suitable to create an additional default constructor. 在极少数情况下,可能适合创建其他默认构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM