为什么我们应该在 dart 中使用 static 关键字来代替抽象?

Ami*_*ngh 2 static static-methods class dart flutter

我正在我的 flutterfire 项目中准备一个类,我想使用一些无法进一步更改的方法,以便我想知道 Dart 中 static 关键字的概念?

Ami*_*rya 10

“静态”意味着成员在类本身而不是类的实例上可用。这就是它的全部含义,并且不用于其他任何用途。static 修改成员

\n

静态方法\n静态方法(类方法)不会对实例进行操作,因此不会访问该实例。然而,它们确实可以访问静态变量。

\n
void main() {\n\n print(Car.numberOfWheels); //here we use a static variable.\n\n//   print(Car.name);  // this gives an error we can not access this property without creating  an instance of Car class.\n\n  print(Car.startCar());//here we use a static method.\n\n  Car car = Car();\n  car.name = 'Honda';\n  print(car.name);\n}\n\nclass Car{\nstatic const numberOfWheels =4;\n  Car({this.name});\n  String name;\n  \n//   Static method\n  static startCar(){\n    return 'Car is starting';\n  }\n  \n}\n
Run Code Online (Sandbox Code Playgroud)\n