简体   繁体   English

Dart,为什么我的 class 在我实现 class 时没有错误,但没有覆盖每个字段,而是从 Mockito 扩展 Mock?

[英]Dart, Why my class has no error when I implements a class without override every field, But extends Mock from Mockito?

I'm in Dart.我在 Dart。

Why my class has no error when I implements a class without override every field, But extends Mock from Mockito?为什么我的 class 在我实现 class 而没有覆盖每个字段但从 Mockito 扩展 Mock 时没有错误?

For example,例如,

class Base {
  void a() {}
  void b() {}
}

// NO ERROR
class ConcreteA implements Base {

  @override
  void a() {}

  @override
  void b() {}

}

// ERROR: Didn't override every field.
class ConcreteB implements Base {

  @override
  void a() {}

}

// NO ERROR
class ConcreteC extends Mock implements Base {

  @override
  void a() {}

}

How could Mockito implement this? Mockito 是如何实现的呢?

The Mock class implements Base 'magically' here because of the Mock overrides the noSuchMethod method . Mock class 在这里“神奇地”实现了 Base,因为 Mock 覆盖了noSuchMethod 方法

  dynamic noSuchMethod(Invocation invocation, [Object /*?*/ returnValue]) {
   // noSuchMethod is that 'magic' that allows us to ignore implementing fields
   // and methods and instead define them later at compile-time per instance.
   // See "Emulating Functions and Interactions" on dartlang.org: goo.gl/r3IQUH

The methods from Base are actually implemented by Mock at compile time.来自 Base 的方法实际上是在编译时由 Mock 实现的。

Thus, since ConcreteC is a subclass of Mock, no error should be shown.因此,由于 ConcreteC 是 Mock 的子类,因此不应显示任何错误。

暂无
暂无

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

相关问题 Dart 实现/扩展抽象 class - Dart implements/extends for abstract class Why isn't my sub class that implements/extends a parent class recognized as a sub type of the parent class in Dart Flutter? - Why isn't my sub class that implements/extends a parent class recognized as a sub type of the parent class in Dart Flutter? 为什么在实现 class 而不覆盖 Dart 中的所有字段时没有错误? - Why is there no error when implementing a class without overriding all its field in Dart? How can I map json data from API into my own dart model without declare any class property in my dart class - How can I map json data from API into my own dart model without declare any class property in my dart class Dart:扩展通用类的限制 - Dart: extends generic class with restrictions Dart:Dart 中的实现和扩展有什么区别 - Dart: What is the difference between implements and extends in Dart Dart 2.0.0访问扩展了另一个的类的变量 - Dart 2.0.0 Access to the variable of a class that extends another Flutter / Dart - 将泛型限制为实现 X 或 Y 接口的类,并且……使用从抽象类继承的方法 - Flutter / Dart - Limit a generic to a class that implements X or Y interface and … use the methods inherited from the abstract class 如何创建 class Searchp 扩展 StatelessWidget 实现 SearchDelegate<an_object> 从头开始</an_object> - how to create class Searchp extends StatelessWidget implements SearchDelegate <an_object> from scratch class 扩展 StatelessWidget 与覆盖构建 function 和常规 class 与我自己的 ZC1C425Z548E68A794F141 有什么区别 - What is the difference between class extends StatelessWidget with override build function and regular class with my own function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM