Last active
September 16, 2021 15:07
-
-
Save DoodleBears/3204034500b1c88eaadc596f4975e5a0 to your computer and use it in GitHub Desktop.
Dart Mixin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dart 会先 Extend 继承 | |
// 之后才会以线性 linear 的方式去 Mixin | |
// linear 意味着最后一个 with 的 mixins 的 functions 会覆盖前面的 | |
class A { | |
void a() { | |
print(runtimeType.toString() + ": a"); | |
} | |
} | |
class A1 { | |
void a() { | |
print(runtimeType.toString() + ": a1"); | |
} | |
} | |
class B_With_A_A1 with A, A1 {} | |
class B1_With_A1_A with A1, A {} | |
class B2_With_A_A1_Override with A, A1 { | |
@override | |
void a() { | |
print(runtimeType.toString() + ": b2"); | |
} | |
} | |
class C { | |
void a() { | |
print(runtimeType.toString() + ": c"); | |
} | |
} | |
class C0_Extends_C extends C {} | |
class C0_Extends_C_Override extends C { | |
@override | |
void a() { | |
print(runtimeType.toString() + ": c0"); | |
} | |
} | |
class C1_Extends_C_With_A_A1 extends C with A, A1 {} | |
class C2_Extends_C_With_A1_A extends C with A1, A {} | |
class C3_Extends_C_With_A_A1_Override extends C with A, A1 { | |
@override | |
void a() { | |
print(runtimeType.toString() + ": c3"); | |
} | |
} | |
void main() { | |
A a = A(); | |
A1 a1 = A1(); | |
B_With_A_A1 b = B_With_A_A1(); | |
B1_With_A1_A b1 = B1_With_A1_A(); | |
B2_With_A_A1_Override b2 = B2_With_A_A1_Override(); | |
C0_Extends_C c0 = C0_Extends_C(); | |
C0_Extends_C_Override c0_override = C0_Extends_C_Override(); | |
C1_Extends_C_With_A_A1 c1 = C1_Extends_C_With_A_A1(); | |
C2_Extends_C_With_A1_A c2 = C2_Extends_C_With_A1_A(); | |
C3_Extends_C_With_A_A1_Override c3 = C3_Extends_C_With_A_A1_Override(); | |
a.a(); | |
a1.a(); | |
print(''); | |
b.a(); | |
b1.a(); | |
b2.a(); | |
print(''); | |
c0.a(); | |
c0_override.a(); | |
c1.a(); | |
c2.a(); | |
c3.a(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使用 DartPad 去运行 https://dartpad.cn/?null_safety=true
观察不同情况下的 output