Dart – 创建新对象会改变旧对象

huangapple go评论104阅读模式
英文:

Dart - Creating new objects changes old objects

问题

在DartPad中运行这段代码,为什么对象a会被后续的声明和实例化相同对象类型的操作更新?如何防止第一个对象被更新?

  1. void main() {
  2. SampleClass a = SampleClass(publicParam: 'A');
  3. print('1. a: ${a.getPrivateProperty}');
  4. SampleClass b = SampleClass(publicParam: 'B');
  5. print('2. a: ${a.getPrivateProperty}');
  6. print('3. b: ${b.getPrivateProperty}');
  7. SampleClass(publicParam: 'C');
  8. print('4. a: ${a.getPrivateProperty}');
  9. print('5. b: ${b.getPrivateProperty}');
  10. }
  11. class SampleClass {
  12. final String publicParam;
  13. static late String _privateProperty;
  14. SampleClass({
  15. required this.publicParam,
  16. }) {
  17. _privateProperty = publicParam;
  18. }
  19. // Getter
  20. String get getPrivateProperty => _privateProperty;
  21. }

输出结果:

  1. 1. a: A
  2. 2. a: B
  3. 3. b: B
  4. 4. a: C
  5. 5. b: C

有人可以详细帮助我吗?非常感谢。我认为这与构造函数有关,但我不太确定。

答:这个问题的原因在于 _privateProperty 是一个 static 静态变量,它属于整个类而不是类的实例。因此,每次你创建一个新的 SampleClass 实例时,都会更新 _privateProperty 的值,因为构造函数中将它设置为了 publicParam 的值。

要解决这个问题,你可以将 _privateProperty 改为实例变量而不是静态变量,这样每个对象都会有自己的 _privateProperty 值,而不会相互影响。代码如下所示:

  1. class SampleClass {
  2. final String publicParam;
  3. late String _privateProperty;
  4. SampleClass({
  5. required this.publicParam,
  6. }) {
  7. _privateProperty = publicParam;
  8. }
  9. // Getter
  10. String get getPrivateProperty => _privateProperty;
  11. }

通过这样的修改,你将能够防止第一个对象被后续对象更新,并且每个对象都会保持其独立的 _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?

  1. void main() {
  2. SampleClass a = SampleClass(publicParam: 'A');
  3. print('1. a: ${a.getPrivateProperty}');
  4. SampleClass b = SampleClass(publicParam: 'B');
  5. print('2. a: ${a.getPrivateProperty}');
  6. print('3. b: ${b.getPrivateProperty}');
  7. SampleClass(publicParam: 'C');
  8. print('4. a: ${a.getPrivateProperty}');
  9. print('5. b: ${b.getPrivateProperty}');
  10. }
  11. class SampleClass {
  12. final String publicParam;
  13. static late String _privateProperty;
  14. SampleClass({
  15. required this.publicParam,
  16. }) {
  17. _privateProperty = publicParam;
  18. }
  19. // Getter
  20. String get getPrivateProperty => _privateProperty;
  21. }

Output:

  1. 1. a: A
  2. 2. a: B
  3. 3. b: B
  4. 4. a: C
  5. 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属性是静态的,这意味着它在类的所有实例之间共享。

我不知道您为什么要另外添加一个私有属性,但假设您需要它,我会像这样重写您的类:

  1. class SampleClass {
  2. final String publicParam;
  3. final String _privateProperty;
  4. SampleClass({
  5. required this.publicParam,
  6. }) : _privateProperty = publicParam;
  7. // Getter
  8. String get getPrivateProperty => _privateProperty;
  9. }
英文:

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:

  1. class SampleClass {
  2. final String publicParam;
  3. final String _privateProperty;
  4. SampleClass({
  5. required this.publicParam,
  6. }) : _privateProperty = publicParam;
  7. // Getter
  8. String get getPrivateProperty => _privateProperty;
  9. }

huangapple
  • 本文由 发表于 2023年7月23日 17:59:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定