【问题标题】:How to override main abstract methods from mixins如何从 mixins 覆盖主要的抽象方法
【发布时间】:2019-05-29 11:33:37
【问题描述】:

正如您在示例中看到的,

  • 我有 Core 类,用于将共享变量/方法等分配到 mixins 中。
  • 用于定义必要方法的抽象类,提供关于 api 的摘要。
  • 用于像提供程序一样导入所有内容的主类。

当然没有任何运行时错误。

这种方法的问题,mixin方法不能识别@override注解。

我想为我的包创建精细、干净的结构。对于这种情况,最好的方法是什么?或者我在做什么?

abstract class AbstractCore {
  void foo();

  void bar();
}

class Core {
  var shared;
}

mixin Feature1 on Core {
  @override // not recognized by syntax of course
  void foo() {
    // something with [shared]
  }
}

mixin Feature2 on Core {
  @override // not recognized
  void bar() {
    // yet another thing with [shared]
  }
}

class Main with Core, Feature1, Feature2 implements AbstractCore {}

你可以接受这样的:

  • 核心:ApiBase(用于共享Client对象、常量、保持dispose方法...)
  • Feature1:比方说与身份验证相关的 Api 调用
  • Feature2:比方说与内容相关的 Api 调用
  • 主要:API 提供者。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    注解对代码的作用没有任何影响。它们仅用于提高可读性和工具性。

    在您的关心下,是分析器在抱怨 @override,因为您没有覆盖任何内容。

    只需删除 @override 装饰器 - 一开始就不需要它。

    【讨论】:

    • 感谢 Remi 的回答,我的目的已经是可读性了。因为它在大型项目中非常有用。我会尝试不使用 mixin 的不同结构,但我认为应该使用 mixin 作为概念。
    • 是的,但在这里使用 @override 是错误的。你没有覆盖任何东西,你正在实施它。
    猜你喜欢
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 2012-12-25
    • 2011-08-31
    相关资源
    最近更新 更多