【发布时间】:2019-09-04 07:36:44
【问题描述】:
我是 Dart 的新手,我想明确这一点。我在 Dart 中创建了两个类,一个是“Person”,另一个是第一个的子类,它被命名为“Employee”。 我创建了一个人的对象。当我将此对象更改为 Employee 类的实例时,没有错。但是当我询问 Employee 内部的参数时,我提出了一个错误。
那么为什么 Dart 允许我访问对象的类,却不允许我访问新类内部的参数?
下面的代码:
void main {
var person = Person(name: "Zozor");
print(person.describe());
person = Employee(taxCode: 'AAB');
person.sayName();
print(person.taxCode);
}
class Person {
Person({this.name, this.age, this.height});
String name;
final int age;
final double height;
String describe() => "Hello, I'm ${this.name}. I'm ${this.age} and I'm ${this.height} meter${this.height == 1 ? '':'s'} tall";
void sayName()=> print("Hello, I'm ${this.name}.");
}
class Employee extends Person {
Employee({String name, int age, double height, this.taxCode, this.salary}) : super(name:name, age: age, height: height);
final String taxCode;
final int salary;
}
【问题讨论】:
标签: dart