mixins 可以访问它们在 Dart 中混合的类的状态吗? [英] Can mixins access the state of the class they're mixed with in Dart?
本文介绍了mixins 可以访问它们在 Dart 中混合的类的状态吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
mixin 可以访问它们在 Dart 中混合的类的状态吗?
Can mixins access the state of the class they're mixed with in Dart?
推荐答案
是的,通过使用 on
关键字.此示例说明:
Yes, through the use of on
key word. This example illustrates:
void main() {
final person = Bob();
print(person.age);
print(person.ageInside);
// output will be 31, 13
}
mixin Comedian on Person {
// no need to declare age variable here, it finds it through `on`
@override
int get ageInside {
return age - 18;
}
}
class Person {
int age;
int get ageInside {
return age;
}
Person(this.age);
}
class Bob extends Person with Comedian {
Bob() : super(31);
}
这篇关于mixins 可以访问它们在 Dart 中混合的类的状态吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文