如果存在具有相同名称的方法的Mixin,那么如何调用Super Class方法 [英] How to call Super Class method if there is a Mixin with a method with the same name
问题描述
代码,此处为要点,将显示 e
。如果我删除覆盖,即从 Baz
中删除输出
,它将打印 w 来自
Bar
的code>。
这使我得出结论,方法优先级是自己的类
->
mixin
->
超类
。
The code, here as a Gist, will print e
. If I remove the override, i.e. remove output
from Baz
, it will print w
from Bar
.
This leads me to the conclusion that the method "priority" is own class
->
mixin
->
super class
.
如果我添加更多的mixin,例如像这样:
If I add more mixins, e.g. like this:
mixin Zoo {
output() {
print('j');
}
}
class Baz extends Foo with Bar, Zoo {
// ...
现在,输出为 j
。如果我在 Bar
和 Zoo
之间交换:
Now, the output is j
. If I swap around Bar
and Zoo
:
class Baz extends Foo with Zoo, Bar {
// ...
现在,输出点再次是 w
。
因此,我将这样定义优先级: 自己的班级
->
最后一次混音
->
倒数第二个混音
->
超一流
。
Now, the outpt is w
again.
Consequently, I would define the priority like this: own class
->
last mixin
->
nth-last mixin
->
super class
.
我有什么办法 control 这种行为,即即使 mixin
具有相同名称的方法,也调用超级调用方法?
Is there any way for me to control this behavior, i.e. call the super call method even when a mixin
has a method with the same name?
您可能还想知道为什么我要这样做,而不仅仅是重命名方法。
振兴所有 State
的方法有 dispose
的方法,如果我有 mixin $也具有
dispose
方法的c $ c>,它将破坏 State
的功能离子性,因为 mixin
的 dispose
方法具有上述优先级。
You might be askin why I would want to do this and not just rename the methods.
Well, in Flutter all State
's have a dispose
method and if I have a mixin
that has dispose
method as well, it will break the State
's functionality because the mixin
's dispose
method takes priority as illustrated above.
super.output
也会调用mixin方法,这就是为什么不起作用。您可以尝试将以下构造函数添加到 Baz
:
super.output
will call the mixin method as well, which is why that does not work. You can try adding the following constructor to Baz
:
Baz() {
super.output();
}
即使这样做有效,由于 dispose
方法是从外部调用的。
Even if this worked, it would not help as the dispose
method in the Flutter case is called from the outside.
推荐答案
在mixins中-声明混合的顺序非常重要。
在将混合应用于类时,
Dart中的Mixins通过创建一个新类来工作,该类将mixin的实现层放在一个超类之上,以创建一个新类-它不是超类的侧面,而是上层,因此在解析查询中没有歧义
源
Mixins in Dart work by creating a new class that layers the implementation of the mixin on top of a superclass to create a new class — it is not "on the side" but "on top" of the superclass, so there is no ambiguity in how to resolve lookups source
class A {
String getMessage() => 'A';
}
class B {
String getMessage() => 'B';
}
class P {
String getMessage() => 'P';
}
class AB extends P with A, B {}
class BA extends P with B, A {}
void main() {
String result = '';
AB ab = AB();
result += ab.getMessage();
BA ba = BA();
result += ba.getMessage();
print(result);
}
AB类和AB类都使用A和B混合扩展了P类,但是以不同的顺序。 A,B和P这三个类都有一个名为getMessage的方法。
Both, AB and BA classes extend the P class with A and B mixins but in a different order. All three A, B and P classes have a method called getMessage.
首先,我们调用AB类的getMessage方法,然后调用BA类的getMessage方法。
First, we call the getMessage method of the AB class, then the getMessage method of the BA class.
输出为 BA
。
这篇关于如果存在具有相同名称的方法的Mixin,那么如何调用Super Class方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!