繁体   English   中英

扩展gridview时,为什么需要在构造函数中调用super?

[英]When extending a gridview, why need to call super in constructor?

我在堆栈溢出中实现了一个像Kishanjvaghela 的 github示例或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()关键字将调用无参数构造函数,所以要调用父类的参数构造函数,必须编写带有参数的super()关键字

看看构造函数链是如何工作的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM