kotlin class constructor 構造方法的使用

6 kotlin之class

kotlin的class比Java更簡潔,構造方法,使用更方便,類似於方法直接寫上參數即可
class Player {
    var name: String = ""

    constructor(fullName: String) {
        this.name = fullName

    }

    fun sayHello() {
        println("hello $name")
    }

    fun getAge(): Int {
        return 18
    }
}

class Match(private val home: String,
            private val away: String, private val data: String) {
    fun getMatchInfo(): String {
        return "$away- $home -$data"
    }
}

fun main() {
    val player = Player("handsome")
    player.sayHello()
    println("${player.name} is ${player.getAge()}")

    val math = Match("China", "building",
            "20200522")
    println(math.getMatchInfo())
}

喜歡可以加Q羣號:874826112,一起學習,成長,交流工作經驗,
記得要點贊,評論哦😯;

更多相關內容請點擊主頁查看,有你想不到的驚喜😂…

簡書: https://www.jianshu.com/u/88db5f15770d

csdn:https://me.csdn.net/beyondforme

掘金:https://juejin.im/user/5e09a9e86fb9a016271294a7

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章