OnClickListener { constructor(context: Context): this(context, null, 0) constructor(context: Context, attrs: AttributeSet?): this(context, attrs, 0) init { LayoutInflater.from(context).inflate(R.layout.recording_bootom_panel, this, true) findViewById<View>(R.id.recording_channel_switch_btn)...
constructor(ctx: String, attr: String) { println("constructor(ctx: String, attr: String): ${ctx}, ${attr}") } } 下面是继承View的构造函数 classMybutton: View { constructor(ctx: String):this(ctx, "default") {}//调用下面的构造函数constructor(ctx: String, attr: String):super("kkqqq")...
classSonconstructor(name:String,age:Int):Father(name,age){override funaction(){println("Son")}} 3 . 子类没有主构造函数 :如果没有主构造函数 , 那么子类必须有次构造函数 , 子类需要在次构造函数中定义需要的变量 , 其中的参数 , 可以直接传递给后面 super ( ) 委托调用的主构造函数 ; 代码语言:jav...
ENclassNonSwipeableViewPager:ViewPager{init{setMyScroller()}constructor(context:Context):super(context...
Kotlin的构造函数分为主构造器(primary constructor)和次级构造器(secondary constructor)。下面我们来看看他们的写法。 一、 Primary Constructor 1. 写法一: class 类名 constructor(形参1, 形参2, 形参3){} eg: class Person constructor(username: String, age: Int){ ...
classMyView@JvmOverloadsconstructor( context: Context, attrs: AttributeSet? =null, defStyleAttr:Int=0 ) : View(context, attrs, defStyleAttr), CoroutineScopebyViewCoroutineScope() { overridefunonDetachedFromWindow(){ super.onDetachedFromWindow()
super(); = name; } } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 如果不需要将构造函数中参数同时作为类属性,也可以写成如下形式(constructor表示构造函数,里面执行初始化的处理): class User { constructor(name:String) ...
constructor(context:Context,attrs:AttributeSet?,defStyleAttr:Int) : super(context,attrs,defStyleAttr) } 可以看出,当实现类无主构造函数时,分别使用了super()去实现了基类的三个构造函数。 存在主构造函数 当存在主构造函数时,主构造函数一般实现基类型中参数最多的构造函数,参数少的构造函数则用this关键字引用即...
constructor(@NotNull context: Context, @StyleRes i: Int) : super(context, i) 必须改成 constructor(@NotNull context: Context, @StyleRes i: Int) : this(context) 也就是说,有主构造函数存在,次构造函数必须引用主构造函数。 所以,如果想在次构造函数中引用super的构造函数,必须去掉主构造函数。
如果派生类没有主构造函数,那么每个次构造函数必须使用super关键字初始化其基类型,或委托给另一个做到这点的构造函数。请注意,在这种情况下,不同的次构造函数可以调用基类型的不同的构造函数: classMyView:View{constructor(ctx:Context):super(ctx)constructor(ctx:Context,attrs:AttributeSet):super(ctx,attrs)} ...