Skip to content

Instantly share code, notes, and snippets.

@fengyfei
Created May 28, 2019 13:38
Show Gist options
  • Save fengyfei/b301589626b9d62075e63a5a40812112 to your computer and use it in GitHub Desktop.
Save fengyfei/b301589626b9d62075e63a5a40812112 to your computer and use it in GitHub Desktop.
[Dart] Chained Methods with Mixin
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);
}
@abserari
Copy link

不错,有点像组合/匿名字段/实现/继承。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment