Created
May 30, 2019 06:47
-
-
Save fengyfei/93782a4c8f5b6e049300a0e3fc185146 to your computer and use it in GitHub Desktop.
[Dart] Getter/Setter Magic
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 VM { | |
Host get host => _host; | |
set host(Host h) { | |
_host = h; | |
} | |
Host _host; | |
void initialize(Host h) { | |
assert(_host == null); | |
_host = h; | |
print('VM initialized'); | |
} | |
} | |
class VirtualBox extends VM { | |
@override | |
void initialize(Host h) { | |
super.initialize(h); | |
assert(host != null); | |
print('VirtualBox initialized'); | |
} | |
} | |
class Host { | |
Host() { | |
print('Host startup...'); | |
} | |
VM get vm => _vm; | |
set vm(VM v) { | |
_vm = v; | |
v.initialize(this); | |
} | |
VM _vm; | |
} | |
void main() { | |
Host machine = Host(); | |
machine.vm = VirtualBox(); | |
print('machine.vm is ${machine.vm.runtimeType}'); | |
print('vm.host is ${machine.vm.host.runtimeType}'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment