kotlin 类继承的语法2

测试:

Kotlin 复制代码
fun main() {
    val xiaoMing = Human("小明")
    xiaoMing.speak()
    
    var chenHaoNan = Human("陈浩南", "我陈浩南出来混,靠的就是三样东西:够狠、够义气、够朋友!")
    chenHaoNan.speak()
}

class Human (var name: String, var zuoyouming: String) { // 主构造函数
    constructor(name: String): this(name, "好好学习,天天向上") { // 辅助构造函数(secondary constructor)
        println("Human secondary constructor...")
//         this.name = name;
//         this.zuoyouming = zuoyouming;
    }
    
    init {
        println("Human init...")
    }
    
    fun speak() {
            println("我是$name, 我的座右铭是:$zuoyouming")
        }
}

打印:

ok. 可以看出int代码块比构造函数调用早。

相关推荐
Kapaseker18 小时前
实战 Compose 中的 IntrinsicSize
android·kotlin
A0微声z3 天前
Kotlin Multiplatform (KMP) 中使用 Protobuf
kotlin
alexhilton3 天前
使用FunctionGemma进行设备端函数调用
android·kotlin·android jetpack
lhDream4 天前
Kotlin 开发者必看!JetBrains 开源 LLM 框架 Koog 快速上手指南(含示例)
kotlin
RdoZam4 天前
Android-封装基类Activity\Fragment,从0到1记录
android·kotlin
Kapaseker4 天前
研究表明,开发者对Kotlin集合的了解不到 20%
android·kotlin
郑州光合科技余经理4 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1234 天前
matlab画图工具
开发语言·matlab
dustcell.4 天前
haproxy七层代理
java·开发语言·前端
norlan_jame4 天前
C-PHY与D-PHY差异
c语言·开发语言