在 Dart 中抽象类是用 abstract 关键字修饰的类。
抽象类的作用是充当普通类的模板,约定一些必要的属性和方法。
abstract class Animal {
String name;
int age;
Animal(this.name, this.age);
void Say();
}
class Dog extends Animal {
late String type;
Dog(String name, int age, this.type) : super(name, age);
@override
void Say() {
print("My name is ${this.name}.");
print("I am ${this.age} years old.");
print("My type is ${this.type}.");
}
}
void main(List<String> args) {
Dog dog = Dog("wangcai", 2, "dog");
dog.Say();
}