Created
December 25, 2018 18:19
-
-
Save dotdoom/9c36dcd5936cdb7d96c002d4ba5b61c3 to your computer and use it in GitHub Desktop.
Default initializer vs constructor
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
class A { | |
String value; | |
A(this.value) { print(value); } | |
String toString() => value; | |
} | |
class B { | |
A a = A('Default'); | |
B(); | |
B.withANull() : this.a = null; | |
B.withAInitList() : this.a = A('Init list'); | |
B.withAParam(this.a); | |
B.withAConstruct() { this.a = A('Construct'); } | |
} | |
void main() { | |
print('>>> B()'); | |
print(B().a); | |
print('>>> B.withANull()'); | |
print(B.withANull().a); | |
print('>>> B.withAInitList()'); | |
print(B.withAInitList().a); | |
print('>>> B.withAParam()'); | |
print(B.withAParam(A('Param')).a); | |
print('>>> B.withAConstruct()'); | |
print(B.withAConstruct().a); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment