问题描述
我知道如何使用关键字 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;
?
解决方法
在这种情况下,您也可以写 this.x
,这只是风格问题。
一般来说,当没有歧义时,建议跳过this
,因为它是隐式调用的。