简体   繁体   English

关于Mixin和Dart类的继承

[英]Inheritance About Mixin and Class Dart

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? 请查看代码,我想知道1.哪个是C超级2.在C级,当我调用'super.method()'时,Mixin执行'void method()'(所以C的超级是Mixin?),然后,当执行'super.method()'时,B执行'void method()'(所以C的超级是B?),为什么?

It's running Dart 2.1.2 它正在运行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): BI/flutter (21340): BI/flutter (21340): mixin I / flutter(21340):BI / flutter(21340):BI / flutter(21340):mixin

The superclass of C is B with Mixin , which is an anonymous class introduced by the extends B with Mixin clause. C的超类是B with Mixin ,它是由extends B with Mixin子句的extends B with Mixin引入的匿名类。

The class C is equivalent to a class declared as follows: C类相当于声明如下的类:

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 . 因此, printSuper不直接击中methodB ,它击中method的超类的CB with Mixin ,并且该方法是从一个复制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 . 然后super.method()_BwithMixin调用super.method() ,并且它的超类是B ,因此它将首先打印mixin然后打印B

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM