Skip to content

Instantly share code, notes, and snippets.

@thara
Created December 21, 2013 12:48
Show Gist options
  • Select an option

  • Save thara/8068926 to your computer and use it in GitHub Desktop.

Select an option

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
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
}
class Person {
final String _name;
Person(this._name);
String get name => _name;
}
// クラス継承
class NewType extends Person {
NewType(String name) : super(name);
}
// 分かりやすくするために冗長な記述にしています
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;
}
}
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);
}
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();
}
class StorablePerson extends Person with Storable {
StorablePerson(String name) : super(name);
}
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