doc: update docs/kotlin.md (#515)
* 新增Kotlin的高阶函数用法 * 新增和调整若干Kotlin示例代码 [新增] 扩展函数 [新增] 运算符重载 [新增] 中缀表达式 [新增] 次构造函数 [新增] Data 数据类 [新增] 内部类 [新增] object 单例类 [修改] 主构造函数示范代码,调整了部分缩进和变量定义。
This commit is contained in:
parent
b8d46f0cee
commit
2f6c263221
211
docs/kotlin.md
211
docs/kotlin.md
@ -678,6 +678,78 @@ fun main(args: Array<String>) {
|
|||||||
这里之所以可以把lambda写在外部,是因为operation是最后一个参数。
|
这里之所以可以把lambda写在外部,是因为operation是最后一个参数。
|
||||||
<!--rehype:className=wrap-text-->
|
<!--rehype:className=wrap-text-->
|
||||||
|
|
||||||
|
### 扩展函数
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
// Kotlin File
|
||||||
|
fun String.lettersCount(): Int {
|
||||||
|
var count = 0
|
||||||
|
// this 相当于我们下面写的字符串具体的内容
|
||||||
|
// for 可以用 forEach 代替
|
||||||
|
for (char in this) {
|
||||||
|
// 判断是不是字母(包括中文)
|
||||||
|
if (char.isLetter()) {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
//不修改 String 类的情况下新增方法
|
||||||
|
println("123demo".lettersCount())
|
||||||
|
// Print: 4
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 运算符重载
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
class Money(var amount: Double)
|
||||||
|
|
||||||
|
// 配合扩展函数,重载运算符 + 即 plus
|
||||||
|
operator fun Money.plus(money: Money): Money {
|
||||||
|
// 把金额相加返回一个新的 Money对象
|
||||||
|
return Money(this.amount + money.amount)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
val appleMoney = Money(10.0)
|
||||||
|
val eggMoney = Money(6.0)
|
||||||
|
// 你没有看错,我们将两个类对象相加了
|
||||||
|
val allMoney = appleMoney + eggMoney
|
||||||
|
println(allMoney.amount)
|
||||||
|
// Print: 16.0
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
这里的 **运算符重载** 依赖于 **扩展函数**
|
||||||
|
<!--rehype:className=wrap-text-->
|
||||||
|
|
||||||
|
### 中缀表达式
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
// infix 定义一个中缀表达式,类似扩展函数那样
|
||||||
|
infix fun LocalDate.formatBy(pattern:String):String{
|
||||||
|
val formatter = DateTimeFormatter.ofPattern(pattern)
|
||||||
|
return this.format(formatter)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
val currentDate = LocalDate.now()
|
||||||
|
println(currentDate formatBy "yyyy-MM-dd")
|
||||||
|
// Print: 2024-02-08
|
||||||
|
|
||||||
|
(1 until 100).forEach {
|
||||||
|
println(it)
|
||||||
|
// Print 1 至 99
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
类
|
类
|
||||||
---
|
---
|
||||||
|
|
||||||
@ -707,20 +779,52 @@ fun main() {
|
|||||||
<!--rehype:wrap-class=col-span-2-->
|
<!--rehype:wrap-class=col-span-2-->
|
||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
class Student(val name: String, val gpa: Double, val semester: String, val estimatedGraduationYear: Int)
|
class Student(
|
||||||
|
val name: String,
|
||||||
|
val gpa: Double,
|
||||||
|
val semester: String,
|
||||||
|
val estimatedGraduationYear: Int
|
||||||
|
)
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
var student = Student("Lucia", 3.95, "Fall", 2022)
|
val student = Student("Lucia", 3.95, "Fall", 2022)
|
||||||
println(student.name)
|
println(student.name)
|
||||||
// Prints: Lucia
|
// Prints: Lucia
|
||||||
println(student.gpa)
|
println(student.gpa)
|
||||||
// Prints: 3.95
|
// Prints: 3.95
|
||||||
println(student.semester)
|
println(student.semester)
|
||||||
// Prints: Fall
|
// Prints: Fall
|
||||||
println(student.estimatedGraduationYear)
|
println(student.estimatedGraduationYear)
|
||||||
// Prints: 2022
|
// Prints: 2022
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 次构造函数
|
||||||
|
<!--rehype:wrap-class=col-span-2-->
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
class Student(
|
||||||
|
val name: String,
|
||||||
|
val gpa: Double,
|
||||||
|
val semester: String,
|
||||||
|
val estimatedGraduationYear: Int
|
||||||
|
) {
|
||||||
|
constructor(name: String, gpa: Double) : this(name, gpa, "Fall", 2024)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
val student = Student("Lucia", 3.95)
|
||||||
|
println(student.name)
|
||||||
|
// Prints: Lucia
|
||||||
|
println(student.semester)
|
||||||
|
// Prints: Fall
|
||||||
|
println(student.estimatedGraduationYear)
|
||||||
|
// Prints: 2024
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
<!--rehype:className=wrap-text-->
|
<!--rehype:className=wrap-text-->
|
||||||
|
|
||||||
### 类示例
|
### 类示例
|
||||||
@ -785,6 +889,93 @@ fun main() {
|
|||||||
```
|
```
|
||||||
<!--rehype:className=wrap-text-->
|
<!--rehype:className=wrap-text-->
|
||||||
|
|
||||||
|
### Data数据类
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
// 默认实现 getter/setter 和 toString 这些方法
|
||||||
|
data class UserInfo(
|
||||||
|
val name: String,
|
||||||
|
val age: Int
|
||||||
|
)
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
|
||||||
|
val userInfo = UserInfo("张三", 20)
|
||||||
|
println(userInfo.name)
|
||||||
|
// 张三
|
||||||
|
println(userInfo.toString())
|
||||||
|
// UserInfo(name=张三, age=20)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 伴生对象
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
// 私有化构造方法
|
||||||
|
class User private constructor(val name: String) {
|
||||||
|
// 伴生对象,相当于一个静态类
|
||||||
|
companion object {
|
||||||
|
fun createUser(name: String): User {
|
||||||
|
return User(name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
// 就像是调用静态方法
|
||||||
|
val user = User.createUser("张三")
|
||||||
|
println(user.name)
|
||||||
|
//Print: 张三
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### 内部类
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
class Outer {
|
||||||
|
val outStr: String = "Outer"
|
||||||
|
// inner 可以让内部类访问外部类
|
||||||
|
inner class Inner {
|
||||||
|
fun printOutStr(){
|
||||||
|
println(outStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
val outer = Outer()
|
||||||
|
outer.Inner().printOutStr()
|
||||||
|
// Print: Outer
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
如果不用inner修饰,会导致Inner类无法使用outStr
|
||||||
|
<!--rehype:className=wrap-text-->
|
||||||
|
|
||||||
|
### object单例类
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
object HttpUtils {
|
||||||
|
|
||||||
|
const val baseUrl = "https://xxxx.com"
|
||||||
|
|
||||||
|
fun getRequest(url: String): String {
|
||||||
|
// 示例代码....
|
||||||
|
return "Result"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
println(HttpUtils.baseUrl)
|
||||||
|
// Print: "https://xxxx.com"
|
||||||
|
HttpUtils.getRequest("xxxxx")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
object类中定义的函数和属性都可以用类名直接引用
|
||||||
|
<!--rehype:className=wrap-text-->
|
||||||
|
|
||||||
另见
|
另见
|
||||||
---
|
---
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user