Appearance
类(面向对象)
类的声明和构造函数
dart
class Person {
String name;
int age;
Person(this.name, this.age); // 这是构造函数,是简化写法,等价于下面的代码
// Person(String name, int age){
// this.name = name;
// this.age = age;
// }
void sayHello() {
print('hello $name, you are $age years old');
}
}继承
dart
class Student extends Person {
int id;
Student(String name, int age, this.id) : super(name, age);
@override
void sayHello() {
print('hello $name, you are $age years old, your id is $id');
}
}工厂构造函数
dart
class Student extends Person {
int id;
factory Student.fromJson(Map<String, dynamic> json) {
return Student(json['name'], json['age'], json['id']);
}
}枚举类
dart
enum Gender {
male,
female,
}
// 枚举类的使用
Gender gender = Gender.male;
print(gender);