英文:
Dart - Creating new objects changes old objects
问题
在DartPad中运行这段代码,为什么对象a
会被后续的声明和实例化相同对象类型的操作更新?如何防止第一个对象被更新?
void main() {
SampleClass a = SampleClass(publicParam: 'A');
print('1. a: ${a.getPrivateProperty}');
SampleClass b = SampleClass(publicParam: 'B');
print('2. a: ${a.getPrivateProperty}');
print('3. b: ${b.getPrivateProperty}');
SampleClass(publicParam: 'C');
print('4. a: ${a.getPrivateProperty}');
print('5. b: ${b.getPrivateProperty}');
}
class SampleClass {
final String publicParam;
static late String _privateProperty;
SampleClass({
required this.publicParam,
}) {
_privateProperty = publicParam;
}
// Getter
String get getPrivateProperty => _privateProperty;
}
输出结果:
1. a: A
2. a: B
3. b: B
4. a: C
5. b: C
有人可以详细帮助我吗?非常感谢。我认为这与构造函数有关,但我不太确定。
答:这个问题的原因在于 _privateProperty
是一个 static
静态变量,它属于整个类而不是类的实例。因此,每次你创建一个新的 SampleClass
实例时,都会更新 _privateProperty
的值,因为构造函数中将它设置为了 publicParam
的值。
要解决这个问题,你可以将 _privateProperty
改为实例变量而不是静态变量,这样每个对象都会有自己的 _privateProperty
值,而不会相互影响。代码如下所示:
class SampleClass {
final String publicParam;
late String _privateProperty;
SampleClass({
required this.publicParam,
}) {
_privateProperty = publicParam;
}
// Getter
String get getPrivateProperty => _privateProperty;
}
通过这样的修改,你将能够防止第一个对象被后续对象更新,并且每个对象都会保持其独立的 _privateProperty
值。
英文:
Running this in DartPad, why does object a
get updated by succeeding declarations and instantiations of the same object type? How do I prevent the first objects from being updated?
void main() {
SampleClass a = SampleClass(publicParam: 'A');
print('1. a: ${a.getPrivateProperty}');
SampleClass b = SampleClass(publicParam: 'B');
print('2. a: ${a.getPrivateProperty}');
print('3. b: ${b.getPrivateProperty}');
SampleClass(publicParam: 'C');
print('4. a: ${a.getPrivateProperty}');
print('5. b: ${b.getPrivateProperty}');
}
class SampleClass {
final String publicParam;
static late String _privateProperty;
SampleClass({
required this.publicParam,
}) {
_privateProperty = publicParam;
}
// Getter
String get getPrivateProperty => _privateProperty;
}
Output:
1. a: A
2. a: B
3. b: B
4. a: C
5. b: C
Can someone please help me as detailed as possible? Thank you so much. I think it's got to do with the constructor but I'm not really sure.
答案1
得分: 1
原因是您的_privateProperty
属性是静态的,这意味着它在类的所有实例之间共享。
我不知道您为什么要另外添加一个私有属性,但假设您需要它,我会像这样重写您的类:
class SampleClass {
final String publicParam;
final String _privateProperty;
SampleClass({
required this.publicParam,
}) : _privateProperty = publicParam;
// Getter
String get getPrivateProperty => _privateProperty;
}
英文:
The cause is that your _privateProperty
property is static, which means it is shared across all instances of the class.
I do not know your motivation of having another private property, but assuming you need it I would rewrite your class like this:
class SampleClass {
final String publicParam;
final String _privateProperty;
SampleClass({
required this.publicParam,
}) : _privateProperty = publicParam;
// Getter
String get getPrivateProperty => _privateProperty;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论