Created
December 21, 2013 12:48
-
-
Save thara/8068926 to your computer and use it in GitHub Desktop.
Dartのクラス継承、Implicit Interface、Mix-inについて ref: http://qiita.com/t_hara/items/11555bd3720912bb0ccc
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 Foo { | |
| String call() => "foo"; | |
| } | |
| class Greeting { | |
| void call(String name) => print("Hello, ${name}"); | |
| } | |
| void main() { | |
| var f = new Foo(); | |
| var g = new Greeting(); | |
| g(f()); // Hello, foo | |
| } |
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 Person { | |
| final String _name; | |
| Person(this._name); | |
| String get name => _name; | |
| } | |
| // クラス継承 | |
| class NewType extends Person { | |
| NewType(String name) : super(name); | |
| } |
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 Person { | |
| final String _name; | |
| Person(this._name); | |
| String get name => _name; | |
| } | |
| // クラス継承 | |
| class NewType extends Person { | |
| NewType(String name) : super(name); | |
| } | |
| // 暗黙的インタフェースを実装 | |
| class MutablePerson implements Person { | |
| String _mutableName; | |
| MutablePerson(this._mutableName); | |
| // Personインタフェースのget nameを実装 | |
| String get name => _mutableName; | |
| String set name(value) { | |
| this._mutableName = value; | |
| } | |
| } |
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 Person { | |
| final String name; | |
| Person(this.name); | |
| } | |
| // クラス継承 | |
| class NewType extends Person { | |
| NewType(String name) : super(name); | |
| } | |
| // 暗黙的インタフェースを実装 | |
| class MutablePerson implements Person { | |
| String name; | |
| MutablePerson(this.name); | |
| } |
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 Person { | |
| final String name; | |
| Person(this.name); | |
| String toString() => name; | |
| } | |
| /// Mix-in | |
| class Storable { | |
| void save() => print(this); | |
| } | |
| // コンストラクタを定義しているため、mix-in適用できない | |
| //class Storable { | |
| // Storable(); | |
| // void save() => print(this); | |
| //} | |
| // Object以外を継承しているため、mix-in適用できない | |
| //class Base{} | |
| //class Storable extends Base { | |
| // void save() => print(this); | |
| //} | |
| // superを参照しているため、mix-in適用できない | |
| //class Storable { | |
| // void save() => print(this); | |
| // String toString() => super.toString(); | |
| //} | |
| /// Mix-in Application | |
| class StorablePerson = Person with Storable; | |
| void main() { | |
| var p = new StorablePerson()..name = "t_hara"; | |
| p.save(); | |
| } |
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 StorablePerson extends Person with Storable { | |
| StorablePerson(String name) : super(name); | |
| } |
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 Person { | |
| final String name; | |
| Person(this.name); | |
| String toString() => name; | |
| } | |
| /// Mix-in | |
| abstract class Storable { | |
| void save() => print(this); | |
| } | |
| /// Mix-in | |
| abstract class Genderable { | |
| Gender gender; | |
| } | |
| class Gender { | |
| static const Gender MALE = const Gender._("MALE"); | |
| static const Gender FEMALE = const Gender._("FEMALE"); | |
| final String _value; | |
| const Gender._(this._value); | |
| String toString() => _value; | |
| } | |
| /// Mix-in | |
| abstract class Locatable { | |
| String location; | |
| } | |
| /// Mix-in Composition | |
| abstract class FamilyRegister = Person with Genderable, Locatable; | |
| /// Mix-in Application | |
| class StorablePerson extends FamilyRegister with Storable { | |
| StorablePerson(String name) : super(name); | |
| String toString() => "${this.name}, ${this.gender}, ${this.location}"; | |
| } | |
| void main() { | |
| var p = new StorablePerson("t_hara") | |
| ..gender = Gender.MALE | |
| ..location = "Aichi"; | |
| p.save(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment