【问题标题】:Flutter/Dart: When is the keyword this used?Flutter/Dart:这个关键字什么时候用的?
【发布时间】:2021-02-15 21:14:33
【问题描述】:

我知道如何使用关键字 this,但我不确定是否存在出于某些原因必须避免使用它的情况。来自dart.dev, chapter constructors的示例:

class Point {
  double x, y;

  Point(this.x, this.y);

  // Named constructor
  Point.origin() {
    x = 0;
    y = 0;
  }
}

他们为什么不写

this.x = 0;

而不是

x = 0;

?

【问题讨论】:

标签: flutter dart this keyword


【解决方案1】:

这种情况下你也可以写this.x,只是风格问题。

一般情况下,如果没有歧义,建议跳过this,因为它是隐式调用的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2010-09-06
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    相关资源
    最近更新 更多