【发布时间】:2019-12-14 07:12:03
【问题描述】:
是否可以用它的超类的实例来构造一个类的实例而不用一个一个地调用每个字段(在下面的代码中,我调用bar.name)?
当我这样做时,Foo foo = bar,我得到
Instance of 'Bar': type 'Bar' is not a subtype of type 'Foo'
不过,Foo 扩展了 bar
void main() {
Bar bar = Bar('bar');
Foo foo2 = Foo(null, bar.name); // Works but need to call every field
print(foo2.name);
Foo foo = bar; // How to make this work ?
}
class Bar{
final String name;
Bar(this.name);
}
class Foo extends Bar{
final int age;
Foo(this.age, String name) :super(name);
}
【问题讨论】:
-
你为什么要这么做?
标签: dart