简体   繁体   English

重载子类的构造函数

[英]Overload the constructor of a subclass

I'm trying to make a constructor that takes 2 elements of type Punto and assigns it to the instance variables, in my superclass I already have a constructor but I want one more in my subclass so first in my subclass I call the superclass constructor and then I try to add one more with the following error: 我正在尝试制作一个构造函数,该构造函数接受2个Punto类型的元素并将其分配给实例变量,在我的超类中我已经有一个构造函数,但是我想在我的子类中再添加一个,因此在我的子类中首先调用超类构造函数,然后然后我尝试再添加一个以下错误:

constructor in class cannot be applied to given types. 类中的构造方法不能应用于给定的类型。

Superclass: 超类:

public class Poligono  implements Figura  {

Punto[] vertici;

public Poligono(Punto[] vertici) throws IndexOutOfBoundsException {
    if(vertici == null || vertici.length<3) {
        throw new IndexOutOfBoundsException();
    } 
    this.vertici = vertici;
}

Subclass: 子类:

package figura;

import punto.Punto;


public class Rettangolo extends Poligono{

   Punto p1;
   Punto p2;

   public Rettangolo(Punto[] vertici) throws IndexOutOfBoundsException {
       super(vertici);
   }

   public Rettangolo(Punto p1, Punto p2) throws NullPointerException{

       if(p1==null || p2==null) throw new NullPointerException();

       this.p1 = p1;
       this.p2 = p2;
   }

in my second constructor i get the error: 在我的第二个构造函数中,我得到了错误:

constructor Poligono in class Poligono cannot be applied to given types;
required: Punto[]
found: no arguments
reason: actual and formal argument lists differ in length

Your public Rettangolo(Punto p1, Punto p2) must call some constructor of the super class Poligono . 您的public Rettangolo(Punto p1, Punto p2)必须调用超类Poligono某些构造函数。 The compiler complains that the only constructor available - Poligono(Punto[] vertici) - doesn't fit the parameters of the second Rettangolo constructor. 编译器抱怨唯一可用的构造函数Poligono(Punto[] vertici) -不适合第二个Rettangolo构造函数的参数。

You have to explicitly call the Poligono constructor with the super(..) call. 您必须使用super(..)调用显式调用Poligono构造函数。

Assuming that the two points passed to that constructor are two opposite corners of a rectangle, and that the Poligono constructor expects an array of vertices, you need something like this : 假设传递给该构造函数的两个点是矩形的两个相对角,并且Poligono构造函数期望一个顶点数组,则需要这样的操作:

public Rettangolo(Punto p1, Punto p2) {

    super (new Punto[]{p1,
                       new Punto (p1.getX(),p2.getY()), 
                       p2, 
                       new Punto (p2.getX(),p1.getY())});

    this.p1 = p1;
    this.p2 = p2;
}

I may have gotten the order of the points or the method names wrong. 我可能弄错了点的顺序或方法名称错误。

Why does this() and super() have to be the first statement in a constructor? 为什么this()和super()必须是构造函数中的第一条语句?

What you can do (besides calling super() ) is 您可以做的(除了调用super()之外)是

public Rettangolo(Punto p1, Punto p2) {
     super(new Punto[] {p1, p2});
}

I'd prefer this solution, because if you use Rettangolo(Punto[]) you do not have any points to work with. 我更喜欢这种解决方案,因为如果您使用Rettangolo(Punto[]) ,则没有任何要点。 So either you read the points out in your first constructor, eg, 因此,您可以在第一个构造函数中读取要点,例如,

public Rettangolo(Punto[] vertici) throws IndexOutOfBoundsException {
   super(vertici);
   p1 = vertici[0];
   p2 = vertici[1];
}

or you do not work with single points and only use the array instead. 否则您将无法使用单点,而只能使用数组。

There is no superclass constructor which will be called if you initialize 'Rettangolo' object. 如果初始化“ Rettangolo”对象,则不会调用任何超类构造函数。

Whenever a object is created the parent class state is initialized before child classes. 每当创建对象时,都会在子类之前初始化父类状态。

The first line of the constructor is either call to the parent constructor or call to the another constructor of the same class, which will call the parent constructor. 构造函数的第一行是调用父构造函数或调用同一类的另一个构造函数,后者将调用父构造函数。

Now your case : 现在你的情况:

public Rettangolo(Punto p1, Punto p2) throws NullPointerException{

   if(p1==null || p2==null) throw new NullPointerException();

   this.p1 = p1;
   this.p2 = p2;

} }

Your constructor is trying to call the default constructor but since there is no such constructor defined, error occurred. 您的构造函数正在尝试调用默认构造函数,但由于未定义此类构造函数,因此发生了错误。

Either you define a default constructor to the parent class and initialize Punto p1, p2 but then vertici[] array will be null. 您可以为父类定义默认的构造函数并初始化Punto p1,p2,但是vertici []数组将为null。

Or you can do 或者你可以做

public Rettangolo(Punto p1, Punto p2) throws NullPointerException{
   super(new Punto[] {p1,p2});
   if(p1==null || p2==null) throw new NullPointerException();

   this.p1 = p1;
   this.p2 = p2;
}

But then it depends on your requirements. 但这取决于您的要求。 You just need to remember that parent class constructor will always be called first. 您只需要记住父类构造函数将始终被首先调用。

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

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