Kotlin物件繼承 次建構函式的初始化

2021-08-20 09:45:53 字數 608 閱讀 7243

package com.init.demo.model

/*** 物件繼承—次建構函式的初始化

*/open class demopersonconstruction1

fun

printinfomation() = println("(name='

$name

', age=

$age

, height=

$height

, likefood='

$likefood

', costbymonth=

$costbymonth

)")}

/***每個次建構函式不需使用super關鍵字對超類的屬性進行初始化

* 具體寫法:constructor 子類(次構造函式引數) :super(父類次構造函式引數){}

*/class demostudentconstruction1 : demopersonconstruction1

}class demoworkerconstruction1 : demopersonconstruction1

}fun

main(args: array)

Kotlin建構函式

當kotlin中的類需要建構函式時,可以有乙個主建構函式和多個次建構函式,可以沒有次建構函式。主建構函式在類名後。常規用法 class person name string 當主建構函式有註解或者可見性修飾符,需加 constructor 關鍵字。class personpublic public ...

Kotlin建構函式及多個建構函式

kotlin建構函式 首先 強調一下 如果下面這樣行 是建構函式的話 class classname name string 那麼每乙個kotlin類只有乙個建構函式 沒有多個建構函式 其實由於 什麼的都是源於國外的 所以在翻譯過程中多說會有些出入 畢竟我們不是翻譯官 而大多數的翻譯官又不懂 實際上...

Kotlin 類與物件之建構函式(四)

關鍵字 class 建構函式 主建構函式 只能有乙個主建構函式,主建構函式為類頭的一部分 在類名之後 class person constructor firstname string 主建構函式不能包含任何 初始化的 可以放到以 init 關鍵字作為字首的初始化塊 class test arg i...