小编Pau*_*aul的帖子

如何在 dart/flutter 中继承静态方法?

Dart/Flutter 是否可以继承静态方法或工厂?或者我是否需要通过创建一个实例来访问该静态方法来解决这个问题?

我的情况是,我想序列化一个对象,但需要访问它们的通用解析函数。

abstract class Foo {

  static Foo parse(); //Error, must have a body

  Foo parse();//No error but need to call Foo().parse(); by creating an instance.
}
Run Code Online (Sandbox Code Playgroud)

我想使用 json 创建,所以创建一个新实例以返回另一个实例是不好的做法并且不利于性能吗?

class InheritedFoo {

  final String string;

  InheritedFoo(this.string);

  @override
  Foo parse() {
    return InheritedFoo("some string");
  }
}
Run Code Online (Sandbox Code Playgroud)

是否可以使用单例来节省性能(调用 InheritedFoo.inst.parse() )?

inheritance static function dart flutter

2
推荐指数
1
解决办法
1996
查看次数

标签 统计

dart ×1

flutter ×1

function ×1

inheritance ×1

static ×1