Implicit super constructor Array() is undefined for default constructor. Must define an explicit constructor

2022-10-14,,,,

因为你的父类已经创建了一个带参的构造函数并且父类中没有无参的构造函数,此时编译器不会为你调用默认的构造函数,

所以子类在继承父类的时候需要在自己的构造函数中显式的调用父类的构造函数,这样才能确保子类在初始化前父类会被实例化。

若果父类中有无参的构造方法,子类中就不做要求,编译器会默认帮你自动调用无参的构造。(黑体字代表构造方法)

package ssm;

public class array {
private int arr[];
private int index;

public array(int len) {
if (len > 0) {
this.arr = new int[len];
} else {
this.arr = new int[1];
}
}

public boolean add(int i) {
if (this.index < this.arr.length) {
this.arr[index] = i;
index++;
return true;
} else {
return false;
}
}

public int[] getarray() {
return this.arr;

}
}

 

package ssm;

public class reversearray extends array {

public reversearray(int len) {
super(len);
}

}

 

《Implicit super constructor Array() is undefined for default constructor. Must define an explicit constructor.doc》

下载本文的Word格式文档,以方便收藏与打印。