跳转到内容
主菜单
主菜单
移至侧栏
隐藏
导航
首页
最近更改
随机页面
MediaWiki帮助
代码酷
搜索
搜索
中文(中国大陆)
外观
创建账号
登录
个人工具
创建账号
登录
未登录编辑者的页面
了解详情
贡献
讨论
编辑“︁
Kotlin类基础
”︁(章节)
页面
讨论
大陆简体
阅读
编辑
编辑源代码
查看历史
工具
工具
移至侧栏
隐藏
操作
阅读
编辑
编辑源代码
查看历史
常规
链入页面
相关更改
特殊页面
页面信息
外观
移至侧栏
隐藏
您的更改会在有权核准的用户核准后向读者展示。
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
= Kotlin类基础 = '''Kotlin类基础'''是面向对象编程(OOP)的核心概念之一。类是对象的蓝图,定义了对象的属性和行为。Kotlin的类系统简洁而强大,支持现代OOP特性,同时减少了样板代码。 == 类的定义 == 在Kotlin中,使用<code>class</code>关键字定义类。最简单的类可以没有任何成员: <syntaxhighlight lang="kotlin"> class EmptyClass </syntaxhighlight> === 类成员 === 类可以包含: * '''构造函数'''(主构造和次构造) * '''属性'''(字段) * '''方法'''(函数) * '''初始化块''' * '''嵌套类''' <syntaxhighlight lang="kotlin"> class Person( val name: String, // 属性 - 只读(val) var age: Int // 属性 - 可读写(var) ) { // 方法 fun greet() { println("Hello, I'm $name and I'm $age years old.") } } </syntaxhighlight> == 构造函数 == Kotlin类可以有: * '''主构造函数''':类头部分声明 * '''次构造函数''':类体内用<code>constructor</code>声明 === 主构造函数 === 主构造函数简洁的语法: <syntaxhighlight lang="kotlin"> class User(val id: Int, val name: String) </syntaxhighlight> 等价于: <syntaxhighlight lang="kotlin"> class User constructor(_id: Int, _name: String) { val id: Int = _id val name: String = _name } </syntaxhighlight> === 次构造函数 === 次构造函数必须委托给主构造函数(直接或间接): <syntaxhighlight lang="kotlin"> class Person(val name: String) { var age: Int = 0 // 次构造函数 constructor(name: String, age: Int) : this(name) { this.age = age } } </syntaxhighlight> == 属性 == Kotlin属性自动生成getter/setter: <syntaxhighlight lang="kotlin"> class Rectangle( val width: Int, // 只读属性(只有getter) var height: Int // 可读写属性(有getter和setter) ) { val area: Int // 自定义getter get() = width * height } </syntaxhighlight> == 初始化顺序 == 类实例化时的初始化顺序: 1. 主构造函数参数 2. 类体内属性初始化器和初始化块 3. 次构造函数体 <mermaid> sequenceDiagram participant MainConstructor participant Properties participant InitBlocks participant SecondaryConstructor MainConstructor->>Properties: 1. 主构造参数 Properties->>InitBlocks: 2. 属性和初始化块 InitBlocks->>SecondaryConstructor: 3. 次构造体 </mermaid> == 实际案例:银行账户 == <syntaxhighlight lang="kotlin"> class BankAccount( val accountNumber: String, initialBalance: Double ) { var balance = initialBalance private set // setter设为私有 fun deposit(amount: Double) { require(amount > 0) { "存款金额必须为正数" } balance += amount } fun withdraw(amount: Double): Boolean { require(amount > 0) { "取款金额必须为正数" } return if (amount <= balance) { balance -= amount true } else { false } } override fun toString() = "账户 $accountNumber 余额: $balance" } // 使用示例 fun main() { val account = BankAccount("123456789", 1000.0) account.deposit(500.0) println(account) // 输出: 账户 123456789 余额: 1500.0 val success = account.withdraw(2000.0) println("取款${if (success) "成功" else "失败"}") // 输出: 取款失败 } </syntaxhighlight> == 类的关系 == Kotlin支持面向对象的基本关系: * '''继承''':使用<code>:</code>符号 * '''实现''':接口同样使用<code>:</code> * '''组合''':通过属性包含其他类实例 <syntaxhighlight lang="kotlin"> open class Animal(val name: String) { // open允许继承 open fun makeSound() = "无声" } class Dog(name: String) : Animal(name) { override fun makeSound() = "汪汪!" } </syntaxhighlight> == 可见性修饰符 == Kotlin提供四种可见性: * <code>public</code>(默认):随处可见 * <code>internal</code>:同一模块内可见 * <code>protected</code>:子类可见 * <code>private</code>:类内可见 == 数据类 == 对于纯数据类,Kotlin提供特殊语法: <syntaxhighlight lang="kotlin"> data class User(val id: Int, val name: String, val email: String) </syntaxhighlight> 数据类自动生成: * <code>equals()</code>/<code>hashCode()</code> * <code>toString()</code> * <code>copy()</code> * 组件函数(用于解构) == 总结 == Kotlin的类系统设计体现了现代语言特性: * 简洁的主构造函数语法 * 属性自动生成访问器 * 明确的初始化顺序 * 丰富的类关系支持 * 数据类的特殊优化 掌握Kotlin类基础是深入理解Kotlin面向对象编程的关键第一步。 [[Category:编程语言]] [[Category:Kotlin]] [[Category:Kotlin面向对象编程]]
摘要:
请注意,所有对代码酷的贡献均被视为依照知识共享署名-非商业性使用-相同方式共享发表(详情请见
代码酷:著作权
)。如果您不希望您的文字作品被随意编辑和分发传播,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)