更新时间:2022-02-04 22:33:36
为类创建构造函数时,不会为该类创建任何默认构造函数。因此,如果你扩展该类,并且如果子类试图调用其超类的no-arg构造函数,则会出现编译时错误。
when you create a constructor for a class, there won't be any default constructor created for that class. so if you extend that class and if the subclass tries to call the no-arg constructor of its super class then there will be an compile-time error.
来演示:
class Parent {
int i;
public Parent(int i) {
this.i=i;
}
}
class Child extends Parent {
int j;
public Child(int i, int j) {
super(i);
this.j=j;
}
public Child(int j) {
// here a call to super() is made, but since there is no no-arg constructor
// for class Parent there will be a compile time error
this.j=j;
}
}
编辑:
回答你的问题,不要将值 arrLength
分配给 arr []
和检查[]
,因为当时arrLength将是 0
。
to answer your question do this, don't assign the value arrLength
to arr[]
and check[]
as arrLength would be 0
at that time.
所以只需要声明它们
int arr[];
boolean check[];
并在将输入分配给 arrLength $ c后的构造函数中$ c>放这些陈述。
and in the constructor after you assign the input to arrLength
put these statements.
arr = new int[arrLength];
check = new boolean [arrLength];