Skip to content

Commit be1b743

Browse files
author
haohao
authored
Update README.md
1 parent 21cd2d3 commit be1b743

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,48 @@ view.setOnClickListener(object : OnClickListener{
669669
}
670670
})
671671

672+
```
673+
---
674+
### 扩展
675+
**Java**
676+
```java
677+
// Java 类不能直接进行扩展
678+
```
679+
**Kotlin**
680+
681+
```kotlin
682+
// 一般扩展
683+
class MyClass {
684+
fun foo() = println("member")
685+
}
686+
687+
fun MyClass.bar() = println("extension")
688+
689+
MyClass().bar()
690+
691+
//输出: extension
692+
693+
```
694+
695+
```kotlin
696+
// 扩展函数与成员函数相同
697+
class MyClass {
698+
699+
fun foo() = println("member")
700+
}
701+
702+
fun MyClass.foo() = println("extension") // 扩展函数与成员函数相同时, 成员函数优先
703+
704+
fun MyClass.foo(para: Int) = println("extension")
705+
706+
MyClass().foo()
707+
708+
MyClass().foo(0)
709+
710+
//输出:
711+
// member
712+
// extension
713+
672714
```
673715
---
674716
## 持续更新中。。。

0 commit comments

Comments
 (0)