android - 相当于 Swift “extensions” 的 Dart 是什么?

标签 android ios swift dart flutter

关键字/保留字 extension 通过使用标准框架中的现有类而不对它们进行子类化,可以使代码更加简洁。 Dart 中是否有允许相同行为的关键字?

extension Double {
    var mm: Double { return self / 1_000.0 }
}

let oneInch = 25.4.mm
print("One inch is \(oneInch) meters")

最佳答案

他们终于来了🥳

从 dart 2.6 版开始(不是 100% 确定)。

class Foo {
  printFoo()  {
    print("Foo");
  }
}

extension Bar on Foo { 
  printBar()  {
    print("Bar");
  }
}

void main() {
  final foo = Foo();
  foo.printFoo();
  foo.printBar();
}

打印:

Foo

Bar

您可以在 dart pad. 中尝试一下

关于android - 相当于 Swift “extensions” 的 Dart 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54849827/

相关文章:

android - 发现任务 ':app:processDebugResources' 配置有问题

ios - X 和 Y 值未应用于标签

iphone - iOS 6 的多个 UIInterfaceOrientations 应用程序

ios - 后退按钮后推送不起作用?

ios - Swift:Class 不能声明为 public 因为它的 Super 类是内部的

java - GetText() 工作正常,但 setText() 会使应用程序崩溃

android - Codenameone 本地 map 离线?

安卓 : How to put cross icon on top of the autocomplete textView

ios - 使 iPad 成为单一应用程序设备(或启动锁定?)

macos - Swift 中的授权