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

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