[英]When extending a gridview, why need to call super in constructor?
I implemented an expandedGridView like this Kishanjvaghela's github example or the answer of Raj008 in stack Overflow.我在堆栈溢出中实现了一个像Kishanjvaghela 的 github示例或Raj008 的答案这样的扩展网格视图。
It works fine, but I wonder why does it need to call super()
in the constructor它工作正常,但我想知道为什么它需要在构造函数中调用
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 */
}
If there is no constructor, java must call parent constructor, isn't it ?如果没有构造函数,java必须调用父构造函数,不是吗?
Because by default(optional) super()
keyword will call no-arg constructor, So to call argumented constructor of parent class it must to write super()
keyword with arguments因为默认情况下(可选)
super()
关键字将调用无参数构造函数,所以要调用父类的参数构造函数,必须编写带有参数的super()
关键字
just see how constructor chaining works看看构造函数链是如何工作的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.