Dart 中的字段分配未按预期工作

问题描述 投票:0回答:1
class FooBase {
  String? name;

  FooBase({required this.name});
}
class Foo  extends FooBase{
  final String name;

  Foo({required this.name}) : super(name: name);
}

void main() {
  final foo = Foo(name: 'foo');
  final fooForm = foo;
  fooForm.name = 'bar';
  print(fooForm.name);
}

我希望这段代码打印“bar”,但它打印“foo”。为什么会这样?

如何让它打印“bar”?

flutter dart oop inheritance
1个回答
0
投票

将任何名为 foo 的内容更改为 bar。您应该提供更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.