Reputation: 91
Please look the code ,i want to know 1.who is C super 2.in the class C ,when i call 'super.method()', Mixin excute 'void method()'(so C's super is Mixin?), then, when excute 'super.method()', B excute 'void method()'(so C's super is B?),why?
It's running Dart 2.1.2
void main() {
C().printSuper();
C().method();
}
abstract class A {
void method() {
print("A");
}
}
class B implements A {
@override
void method() {
print("B");
}
}
mixin Mixin on A {
@override
void method() {
super.method();
print("mixin");
}
}
class C extends B with Mixin {
void printSuper() {
super.method();
}
}
print message:
I/flutter (21340): B
I/flutter (21340): mixin
I/flutter (21340): B
I/flutter (21340): mixin
i expected the output print message:
I/flutter (21340): B I/flutter (21340): B I/flutter (21340): mixin
Upvotes: 1
Views: 1047
Reputation: 71653
The superclass of C
is B with Mixin
, which is an anonymous class introduced by the extends B with Mixin
clause.
The class C
is equivalent to a class declared as follows:
class _BwithMixin extends B implements Mixin {
void method() {
super.method();
print("mixin");
}
}
class C extends _BwithMixin {
void printSuper() {
super.method();
}
}
So, the printSuper
doesn't directly hit the method
of B
, it hits the method
of the superclass of C
which is B with Mixin
, and that method is the one copied from Mixin
. It will then call super.method()
from _BwithMixin
, and the superclass of that is B
, so it will first print mixin
and then B
.
Upvotes: 1