美文网首页
kotlin 继承(extend)的2种写法

kotlin 继承(extend)的2种写法

作者: alphet | 来源:发表于2019-03-04 15:47 被阅读0次

kotlin 继承有2种写法
第一种 ,多个构造函数继承,变量数不同的constructor,通过this关联,
最后一个则super到父类


import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout

class ExpandableView:FrameLayout{
    constructor(c:Context):
            this(c,null){
    }
    constructor(context: Context?, attrs: AttributeSet?) :
            this(context, attrs,0){

    }
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) :
            this(context, attrs, defStyleAttr,0){

    }
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) :
            super(context, attrs, defStyleAttr, defStyleRes){

    }
}

第二种 , 只有1个构造函数,比如

import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout

class ExpandableView(c:Context):FrameLayout(c){

}


或者

import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout


class ExpandableView(
context: Context?, 
attrs: AttributeSet?, 
defStyleAttr: Int,
 defStyleRes: Int): FrameLayout(
context, attrs, defStyleAttr, defStyleRes){
}


相关文章

网友评论

      本文标题:kotlin 继承(extend)的2种写法

      本文链接:https://www.haomeiwen.com/subject/hisiuqtx.html