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

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
月光博客
月光博客
Martin Fowler
Martin Fowler
人人都是产品经理
人人都是产品经理
有赞技术团队
有赞技术团队
Hugging Face - Blog
Hugging Face - Blog
T
Tailwind CSS Blog
爱范儿
爱范儿
博客园_首页
Last Week in AI
Last Week in AI
N
Netflix TechBlog - Medium
NISL@THU
NISL@THU
H
Help Net Security
A
Arctic Wolf
K
Kaspersky official blog
Y
Y Combinator Blog
G
GRAHAM CLULEY
T
Threatpost
I
Intezer
Attack and Defense Labs
Attack and Defense Labs
Jina AI
Jina AI
Recent Announcements
Recent Announcements
D
Docker
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
www.infosecurity-magazine.com
www.infosecurity-magazine.com
GbyAI
GbyAI
The Hacker News
The Hacker News
C
Cyber Attacks, Cyber Crime and Cyber Security
Help Net Security
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog
F
Fortinet All Blogs
S
Security Affairs
Security Archives - TechRepublic
Security Archives - TechRepublic
量子位
AWS News Blog
AWS News Blog
Google DeepMind News
Google DeepMind News
罗磊的独立博客
V
Vulnerabilities – Threatpost
阮一峰的网络日志
阮一峰的网络日志
T
Tor Project blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
H
Hacker News: Front Page
Security Latest
Security Latest
The Cloudflare Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
M
MIT News - Artificial intelligence
B
Blog
云风的 BLOG
云风的 BLOG

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