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

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

最佳答案

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

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

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';
  }
  
}

https://stackoverflow.com/questions/66594141/

相关文章:

github - 如何在两个句子之间添加 Git Readme 中的 Tab 空格?

r - 合并和删除*文件之间的冗余行

database - 使用golang从postgres中的csv批量插入而不使用for循环

regex - 为电话号码编写正则表达式

postgresql - 我想用 mikro-orm 插入,但它找不到我的表 :c (TableNo

javascript - typescript :为什么我们不能为泛型类型分配默认值?

git - 使用 git rev-parse 的错误提交哈希

python - 如何在 Python 中将月份名称生成为列表?

c++ - 如果 new 运算符失败,是否可以将指针设置为 null?

html - 将样式添加到 mat-autocomplete 的 mat-option