扩展gridview时,为什么要在构造函数中调用super?
When extending a gridview, why need to call super in constructor?
我在 stack Overflow 中实现了这样的扩展网格视图 Kishanjvaghela's github example or the answer of Raj008。
可以正常工作,但我想知道为什么它需要在构造函数中调用super()
public class ExpandableHeightGridView extends GridView {
boolean expanded = false;
public ExpandableHeightGridView(Context context) {
super(context);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/* more code here */
}
如果没有构造函数,java必须调用父构造函数,不是吗?
因为默认情况下(可选)super()
关键字将调用 no-arg 构造函数,所以要调用父 class 的带参数构造函数,必须编写带参数的 super()
关键字
看看构造函数链是如何工作的
我在 stack Overflow 中实现了这样的扩展网格视图 Kishanjvaghela's github example or the answer of Raj008。
可以正常工作,但我想知道为什么它需要在构造函数中调用super()
public class ExpandableHeightGridView extends GridView {
boolean expanded = false;
public ExpandableHeightGridView(Context context) {
super(context);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/* more code here */
}
如果没有构造函数,java必须调用父构造函数,不是吗?
因为默认情况下(可选)super()
关键字将调用 no-arg 构造函数,所以要调用父 class 的带参数构造函数,必须编写带参数的 super()
关键字
看看构造函数链是如何工作的