flutter 中 Streams 的 ( get ) 关键字的含义
the meaning of the ( get ) keyword with Streams in flutter
Sink<List<FoodItem>> get listSink => _listController.sink;
有人可以向我解释一下 (get) 的作用吗?
这是默认的接收器功能
关键词get
在dart中表示getter
更具体地说,getter 是 class 接口的一部分,它允许您从 class 实例中检索特定值。在 Dart 中,您几乎可以将其视为不带参数的方法,并使用类似于从 class 实例访问字段值(即属性)的语法。
例如,
void main() {
Item myItem = Item(initialCost: 1000);
print(myItem.cost); // 2002
myItem.cost = 100; // Error because there is no setter 'cost'
}
class Item{
int _privateCost;
int initialCost;
int get cost {
return _privateCost * 2;
};
// constructor
Item({this.initialCost}){
_privateCost = initialCost + 1;
}
}
这是 dart 中 getter 方法的语法。
您有一个由 _
定义的私有参数。每个以下划线开头的变量都被认为是私有的,不能在 class 之外读取。来自文档:
Unlike Java, Dart doesn’t have the keywords public, protected, and
private. If an identifier starts with an underscore _, it’s private to
its library.
getter 语法类似于:
Return type
get myVarName => 你想要的私有值 return
想象一下这很简单class:
class A {
int _n = 42;
int get nValue => _n;
}
为了从 class A 访问 n
的值,我需要使用 getter 因为 n
是私有的。
A myClass = A();
A.n; // Error as n is private
A.nValue; // Return 42.
添加到@Maxouille 的回答:
如果您在 Java 中创建 属性 public,然后决定将其设为私有(因为您必须在 get/set 方法中添加一些额外的逻辑), 调用语法会改变——例如而不是 A.nValue++;
你会得到 A.setNValue(A.getNValue()++);
。
这就是为什么 Java 中的约定是从一开始就将所有属性设为私有,并用 get/set 方法包装它们。这最终得到一堆什么都不做的代码,但是 'just in case'.
这个漂亮的语法使您能够使用 get/set 包装器方法轻松地将 public 属性转换为私有属性 - 无需更改调用代码。
在上面的示例中 - 属性 nValue
可以作为一个简单的 int 属性 开始。一旦决定将其设为私有 - 就像上面的示例一样更改 class:创建一个新的私有变量 _n
并将 nValue 转换为 get/set 属性:
int get nValue => _n;
set nValue(int newValue) => _n=newValue;
您的调用代码将保持不变nValue++;
Sink<List<FoodItem>> get listSink => _listController.sink;
有人可以向我解释一下 (get) 的作用吗?
这是默认的接收器功能
关键词get
在dart中表示getter
更具体地说,getter 是 class 接口的一部分,它允许您从 class 实例中检索特定值。在 Dart 中,您几乎可以将其视为不带参数的方法,并使用类似于从 class 实例访问字段值(即属性)的语法。
例如,
void main() {
Item myItem = Item(initialCost: 1000);
print(myItem.cost); // 2002
myItem.cost = 100; // Error because there is no setter 'cost'
}
class Item{
int _privateCost;
int initialCost;
int get cost {
return _privateCost * 2;
};
// constructor
Item({this.initialCost}){
_privateCost = initialCost + 1;
}
}
这是 dart 中 getter 方法的语法。
您有一个由 _
定义的私有参数。每个以下划线开头的变量都被认为是私有的,不能在 class 之外读取。来自文档:
Unlike Java, Dart doesn’t have the keywords public, protected, and private. If an identifier starts with an underscore _, it’s private to its library.
getter 语法类似于:
Return type
get myVarName => 你想要的私有值 return
想象一下这很简单class:
class A {
int _n = 42;
int get nValue => _n;
}
为了从 class A 访问 n
的值,我需要使用 getter 因为 n
是私有的。
A myClass = A();
A.n; // Error as n is private
A.nValue; // Return 42.
添加到@Maxouille 的回答:
如果您在 Java 中创建 属性 public,然后决定将其设为私有(因为您必须在 get/set 方法中添加一些额外的逻辑), 调用语法会改变——例如而不是 A.nValue++;
你会得到 A.setNValue(A.getNValue()++);
。
这就是为什么 Java 中的约定是从一开始就将所有属性设为私有,并用 get/set 方法包装它们。这最终得到一堆什么都不做的代码,但是 'just in case'.
这个漂亮的语法使您能够使用 get/set 包装器方法轻松地将 public 属性转换为私有属性 - 无需更改调用代码。
在上面的示例中 - 属性 nValue
可以作为一个简单的 int 属性 开始。一旦决定将其设为私有 - 就像上面的示例一样更改 class:创建一个新的私有变量 _n
并将 nValue 转换为 get/set 属性:
int get nValue => _n;
set nValue(int newValue) => _n=newValue;
您的调用代码将保持不变nValue++;