Created
May 28, 2019 13:38
-
-
Save fengyfei/b301589626b9d62075e63a5a40812112 to your computer and use it in GitHub Desktop.
[Dart] Chained Methods with Mixin
This file contains hidden or 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
abstract class System { | |
System() { | |
initialize(); | |
} | |
void initialize() { | |
print('System boot start...'); | |
} | |
} | |
mixin Disk on System { | |
static Disk get instance => _instance; | |
static Disk _instance = null; | |
@override | |
void initialize() { | |
super.initialize(); | |
_instance = this; | |
print('Disk initializing now'); | |
} | |
void capacity() { | |
print('Disk ---> 40GB'); | |
} | |
} | |
mixin Processor on System { | |
static Processor get instance => _instance; | |
static Processor _instance = null; | |
@override | |
void initialize() { | |
super.initialize(); | |
_instance = this; | |
print('Processor initializing now'); | |
} | |
void cores() { | |
print('Processor ---> 4 cores'); | |
} | |
} | |
class Linux extends System with Processor, Disk { | |
} | |
void main() { | |
Linux(); | |
Disk.instance.capacity(); | |
Processor.instance.cores(); | |
print(Disk.instance.runtimeType); | |
print(Processor.instance.runtimeType); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
不错,有点像组合/匿名字段/实现/继承。