我試圖做一個構造函數,它需要2個類型爲Punto
的元素並將其指定給實例變量,在我的超類中我已經有了一個構造函數,但是我希望在我的子類中再多一個在我的子類我調用父類的構造函數,然後我嘗試添加一個多與以下錯誤:重載子類的構造函數
constructor in class cannot be applied to given types.
超類:
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'陣列。 –