【发布时间】:2016-01-14 12:51:55
【问题描述】:
我正在尝试创建一个构造函数,它采用Punto 类型的 2 个元素并将其分配给实例变量,在我的超类中我已经有一个构造函数,但我想要在我的子类中再添加一个,所以首先在我的子类中我调用超类构造函数,然后我尝试再添加一个,但出现以下错误:
类中的构造函数不能应用于给定类型。
超类:
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;
}
子类:
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;
}
在我的第二个构造函数中,我得到了错误:
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
【问题讨论】:
-
请注意,您几乎肯定希望在构造函数中获取
vertici的副本:所谓的防御性副本 可以保护您的Poligono类免受后续更改vertici数组。
标签: java inheritance constructor