惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
Blog — PlanetScale
Blog — PlanetScale
腾讯CDC
aimingoo的专栏
aimingoo的专栏
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans
J
Java Code Geeks
G
Google Developers Blog
N
Netflix TechBlog - Medium
Vercel News
Vercel News
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
I
InfoQ
Stack Overflow Blog
Stack Overflow Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
罗磊的独立博客
GbyAI
GbyAI
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - y丶innocence

临时111 RecyclerView 数据多时无法滑动:ConstraintLayout 约束高度修复笔记 Android Kotlin OkHttp3 WebSocket 长连接与 Gson 数据解析系统笔记 Android + Kotlin + OkHttp WebSocket 相关概念与使用流程笔记(TLS/证书 + 鉴权/会话) AI对话导出markdown格式流程 代理转发 分享文件 面向全球的app的excel导出和kotlin IO原理 安卓导出笔记(未整理) java&kotlin listener 0.7 动画 0.4 View 工作流程 0.3 view 滑动冲突 13. Jetpack 0. 安卓开发艺术探索参考资料 12. Material Design 7. 持久化技术 5. Fragment java 基础 4. UI 开发 3. Activity 2.3 Kotlin高级 2.1 Kotlin基础 1. Android简介 [OpenJudge] 反正切函数的应用 (枚举)(数学) [OpenJudge] 摘花生 (模拟)
2.2 Kotlin 面向对象
y丶innocence · 2026-01-09 · via 博客园 - y丶innocence

2. Kotlin 面向对象

1. 类

fun main() {
//    val s = Student("180", 9, "liao", 26)
//    s.eat()
    val ss = Student()  // 构造函数调用顺序
    doStudy(ss)
}

open class Person(var name: String, var age: Int) {      // 加上 open 之后才允许被继承
    fun eat() {
        println("$name is eating. He is $age years old.")
    }
}

class FinalStudent(val sno: String = "sno", val grade : Int = 9, name : String = "name", age : Int = 26) :    // 只使用一个主构造函数来满足各种传参方式
        Person(name, age), Study {

        }

class Student(val sno: String, val grade: Int, name: String, age: Int) :
        Person(name, age), Study {  // 主构造函数,没有函数体
    init {                   // 没有函数体但是可以写 init 逻辑
        println("constructor0")
    }
    constructor(name: String, age: Int) : this("", 0, name, age) { println("constructor1") }     // 次构造函数1,调用主构造函数
    constructor() : this("", 0) {println("constructor2")}                                        // 次构造函数2,调用次构造函数1

    override fun readingBooks() {
        println("$name is reading")
    }
    override fun doHomeworks() {
        println("$name is doing homework")
    }
}

class Teacher : Person {                                                // 只有次构造函数,因此不需要()
    constructor(name : String, age : Int) : super(name, age) {}         // 没有主构造函数,调用父类构造函数
}

interface Study {           // 接口类不能实例化   但是似乎没有纯虚函数的概念,即必须实现所有纯虚函数才能实例化
    fun readingBooks(){}
    fun doHomeworks(){
        println("do homework")
    }
}

fun doStudy(study: Study?){   // 多态思想
    study?.readingBooks()
    study?.doHomeworks()
}
  • 未完成部分
    • 可见性修饰符
    • 继承
    • 枚举类
    • 数据
    • 抽象类(那接口类的意义是什么呢)
    • 封装等等