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

推荐订阅源

人人都是产品经理
人人都是产品经理
Stack Overflow Blog
Stack Overflow Blog
S
SegmentFault 最新的问题
博客园 - 司徒正美
aimingoo的专栏
aimingoo的专栏
U
Unit 42
GbyAI
GbyAI
B
Blog RSS Feed
博客园 - Franky
L
LangChain Blog
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 三生石上(FineUI控件)
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
Last Week in AI
Last Week in AI
阮一峰的网络日志
阮一峰的网络日志
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - 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()
}
  • 未完成部分
    • 可见性修饰符
    • 继承
    • 枚举类
    • 数据
    • 抽象类(那接口类的意义是什么呢)
    • 封装等等