首页 > 解决方案 > 为什么我们应该在飞镖中使用静态关键字来代替抽象?

问题描述

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

标签: flutterclassdartstaticstatic-methods

解决方案


“静态”表示成员在类本身而不是类的实例上可用。这就是它的全部含义,它不用于其他任何事情。static 修改成员

静态方法 静态方法(类方法)不对实例进行操作,因此无权访问它。但是,它们确实可以访问静态变量。

void main() {

 print(Car.numberOfWheels); //here we use a static variable.

//   print(Car.name);  // this gives an error we can not access this property without creating  an instance of Car class.

  print(Car.startCar());//here we use a static method.

  Car car = Car();
  car.name = 'Honda';
  print(car.name);
}

class Car{
static const numberOfWheels =4;
  Car({this.name});
  String name;
  
//   Static method
  static startCar(){
    return 'Car is starting';
  }
  
}

推荐阅读