我浏览了一下language documentation,发现Google Dart似乎不支持混合(接口中没有方法体,没有多重继承,没有类似Ruby的模块).关于这一点,我说得对吗?或者,有没有其他方法可以在DART中实现类似于Mixin的功能?
我浏览了一下language documentation,发现Google Dart似乎不支持混合(接口中没有方法体,没有多重继承,没有类似Ruby的模块).关于这一点,我说得对吗?或者,有没有其他方法可以在DART中实现类似于Mixin的功能?
我很高兴地告诉大家,现在的答案是肯定的!
Mixin实际上只是子类和超类之间的增量.然后,您可以将该增量"混合"到另一个类中.
例如,考虑这个抽象类:
abstract class Persistence {
void save(String filename) {
print('saving the object as ${toJson()}');
}
void load(String filename) {
print('loading from $filename');
}
Object toJson();
}
然后,您可以将其混合到其他类中,从而避免了对继承树的污染.
abstract class Warrior extends Object with Persistence {
fight(Warrior other) {
// ...
}
}
class Ninja extends Warrior {
Map toJson() {
return {'throwing_stars': true};
}
}
class Zombie extends Warrior {
Map toJson() {
return {'eats_brains': true};
}
}
对混合定义的限制包括:
一些额外的阅读material :