Skip to content

Commit b935a35

Browse files
committed
doc: update docs/kotlin.md
1 parent e1fad67 commit b935a35

File tree

1 file changed

+47
-43
lines changed

1 file changed

+47
-43
lines changed

docs/kotlin.md

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ fun main() {
1717
main() 函数是每个 Kotlin 程序的起点,在执行之前必须包含在代码中
1818

1919
### 打印声明
20+
<!--rehype:wrap-class=row-span-2-->
2021

2122
```kotlin
2223
println("Greetings, earthling!")
@@ -31,6 +32,7 @@ Take me to your leader.
3132
```
3233

3334
### 注释
35+
<!--rehype:wrap-class=row-span-2-->
3436

3537
```kotlin
3638
// 这是单行注释
@@ -109,7 +111,7 @@ println(monument.length)
109111
```
110112

111113
### 字符转义序列
112-
<!--rehype:wrap-class=row-span-2-->
114+
<!--rehype:wrap-class=row-span-3-->
113115

114116
```kotlin
115117
print("\"Excellent!\" I cried. \"Elementary,\" said he.")
@@ -149,6 +151,7 @@ print("\"Excellent!\" I cried. \"Elementary,\" said he.")
149151
```
150152

151153
### 增强赋值运算符
154+
<!--rehype:wrap-class=row-span-2-->
152155

153156
```kotlin
154157
var batteryPercentage = 80
@@ -267,6 +270,7 @@ println(shorts && sunny) // false
267270
```
268271

269272
### 或运算符:||
273+
<!--rehype:wrap-class=row-span-2-->
270274

271275
```kotlin
272276
var late = true
@@ -295,6 +299,7 @@ println(!full) // true
295299
```
296300

297301
### 评估顺序
302+
<!--rehype:wrap-class=row-span-2-->
298303

299304
```kotlin
300305
!true && (false || true) // false
@@ -311,6 +316,16 @@ println(!full) // true
311316
*/
312317
```
313318

319+
### 等式运算符
320+
321+
```kotlin
322+
var myAge = 22
323+
var sisterAge = 21
324+
325+
myAge == sisterAge // false
326+
myAge !== sisterAge // true
327+
```
328+
314329
### 嵌套条件
315330

316331
```kotlin
@@ -356,16 +371,6 @@ if (height in 1..53) {
356371
```
357372
<!--rehype:className=wrap-text-->
358373

359-
### 等式运算符
360-
361-
```kotlin
362-
var myAge = 22
363-
var sisterAge = 21
364-
365-
myAge == sisterAge // false
366-
myAge !== sisterAge // true
367-
```
368-
369374
Collections
370375
---
371376

@@ -608,10 +613,11 @@ fun main() {
608613

609614
### 简单的高阶函数
610615
<!--rehype:wrap-class=col-span-2-->
616+
611617
```kotlin
612-
//注意啦,这里的num1AndNum2有个operation,它是接收了一个函数作为形参
618+
// 注意啦,这里的 num1AndNum2 有个 operation,它是接收了一个函数作为形参
613619
fun num1AndNum2(num1: Int, num2: Int, operation: (Int, Int) -> Int): Int {
614-
//让我们试着向operation传入参数
620+
// 让我们试着向 operation 传入参数
615621
return operation(num1, num2)
616622
}
617623

@@ -622,14 +628,11 @@ fun plus(num1: Int, num2: Int): Int {
622628
fun main(args: Array<String>) {
623629
val total = num1AndNum2(100, 200, ::plus)
624630
println(total)//300
625-
//怎么样?我们利用传入一个函数来充当另一个函数的参数
631+
// 怎么样?我们利用传入一个函数来充当另一个函数的参数
626632
}
627-
628633
```
629634

630-
还记得我们怎么在Java中用接口吗?
631-
632-
试着用函数参数简化它
635+
还记得我们怎么在 Java 中用接口吗?试着用函数参数简化它
633636

634637
<!--rehype:className=wrap-text-->
635638

@@ -678,20 +681,6 @@ fun main(args: Array<String>) {
678681
679682
---
680683

681-
### 类示例
682-
683-
```kotlin
684-
// 具有包含默认值的属性的类
685-
class Student {
686-
var name = "Lucia"
687-
var semester = "Fall"
688-
var gpa = 3.95
689-
}
690-
691-
// 没有类体的简写语法
692-
class Student
693-
```
694-
695684
### 类实例
696685

697686
```kotlin
@@ -715,6 +704,7 @@ fun main() {
715704
```
716705

717706
### 主构造函数
707+
<!--rehype:wrap-class=col-span-2-->
718708

719709
```kotlin
720710
class Student(val name: String, val gpa: Double, val semester: String, val estimatedGraduationYear: Int)
@@ -733,24 +723,22 @@ fun main() {
733723
```
734724
<!--rehype:className=wrap-text-->
735725

736-
### 初始化块
726+
### 类示例
737727

738728
```kotlin
739-
class Student(val name: String, val gpa: Double, val semester: String, val estimatedGraduationYear: Int) {
740-
init {
741-
println("$name has ${estimatedGraduationYear - 2020} years left in college.")
742-
}
729+
// 具有包含默认值的属性的类
730+
class Student {
731+
var name = "Lucia"
732+
var semester = "Fall"
733+
var gpa = 3.95
743734
}
744735

745-
fun main() {
746-
var student = Student("Lucia", 3.95, "Fall", 2022)
747-
// Prints: Lucia has 2 years left in college.
748-
}
736+
// 没有类体的简写语法
737+
class Student
749738
```
750-
<!--rehype:className=wrap-text-->
751739

752740
### 成员函数
753-
<!--rehype:wrap-class=col-span-2-->
741+
<!--rehype:wrap-class=col-span-2 row-span-2-->
754742

755743
```kotlin
756744
class Student(val name: String, val gpa: Double, val semester: String, val estimatedGraduationYear: Int) {
@@ -781,6 +769,22 @@ fun main() {
781769
```
782770
<!--rehype:className=wrap-text-->
783771

772+
### 初始化块
773+
774+
```kotlin
775+
class Student(val name: String, val gpa: Double, val semester: String, val estimatedGraduationYear: Int) {
776+
init {
777+
println("$name has ${estimatedGraduationYear - 2020} years left in college.")
778+
}
779+
}
780+
781+
fun main() {
782+
var student = Student("Lucia", 3.95, "Fall", 2022)
783+
// Prints: Lucia has 2 years left in college.
784+
}
785+
```
786+
<!--rehype:className=wrap-text-->
787+
784788
另见
785789
---
786790

0 commit comments

Comments
 (0)