[英]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.