美文网首页
kotlin class constructor的使用

kotlin class constructor的使用

作者: 张漂亮1号 | 来源:发表于2020-05-22 15:10 被阅读0次

    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())
    }
    
    image

    喜欢可以加Q群号:874826112,一起学习,成长,交流工作经验,
    记得要点赞,评论哦😯;

    更多相关内容请点击主页查看,有你想不到的惊喜😂....

    简书: https://www.jianshu.com/u/88db5f15770d

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

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

    相关文章

      网友评论

          本文标题:kotlin class constructor的使用

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