File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -669,6 +669,48 @@ view.setOnClickListener(object : OnClickListener{
669
669
}
670
670
})
671
671
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
+
672
714
```
673
715
---
674
716
## 持续更新中。。。
You can’t perform that action at this time.
0 commit comments