继承
@override
表示覆写了父类里的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| e.g.
class Person { String name; int age;
bool get isAdult => age > 18;
String run() { return 'Name is $name , age is $age'; } }
import 'package:my_flutter/person.dart';
void main() { var person = new Student();
person.age = 20;
person.name = 'Tom';
print(person.isAdult);
person.study();
print(person.run()); }
class Student extends Person { void study() { print('study...'); }
@override bool get isAdult => age > 15; }
|
继承中的构造方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| e.g.
void main() { var student = new Student();
print(student); }
class Person { String name; int age;
Person(){ print('这就是无名无参的构造方法'); } }
class Student extends Person { void study() { print('study...'); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| e.g.
void main() { var student = new Student('Tom');
print(student.name); }
class Person { String name; int age;
Person(this.name);
Person.withName(this.name); }
class Student extends Person { Student(String name) : super(name);
void study() { print('study...'); } }
|
构造方法的执行顺序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| e.g.
void main() { var student = new Student('Tom','Male');
print(student.name); print(student.gender); }
class Person { String name; int age;
Person(this.name);
Person.withName(this.name); }
class Student extends Person { final gender;
Student(String name, String g) : gender = g, super(name);
void study() { print('study...'); } }
|
抽象类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| e.g.
void main() { var student = new Student(); student.run(); }
abstract class Person { void run(); }
class Student extends Person { @override void run() { print('run....'); } }
|
接口
类和接口是统一的 类就是接口
每个类都隐式的定义一个包含所有实例成员的接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| e.g.
void main() { var student = new Student(); student.run(); }
class Person { String name; int age;
void run(){ print('run...'); } }
class Student implements Person{ @override int age;
@override String name;
@override void run() { } }
|
建议使用抽象类的形式来作为接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| e.g.
void main() { var student = new Student(); student.run(); }
abstract class Person { void run(); }
class Student implements Person{ @override void run() { print('student run...'); } }
|
Mixins
Mixins
类似于多继承,是在多类继承中重用一个类代码的方式
以下为 D 同时继承 A B C 类的栗子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| e.g.
void main() { var d = new D(); d.a(); }
class A { void a() { print('A.a()...'); } }
class B { void b() { print('B.b()...'); } }
class C { void c() { print('C.c()...'); } }
class D extends A with B, C {}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| e.g.
abstract class Engine { void work(); }
class OilEngine implements Engine { @override void work() { print('work with oil...'); } }
class ElectricEngine implements Engine { @override void work() { print('work with electric...'); } }
class Tyre { String name;
void run() { print('run...'); } }
class Car = Tyre with OilEngine;
// 完整写法 更像是类的组装 class Cars extends Tyre with OilEngine{
}
class Bus = Tyre with ElectricEngine;
|
操作符覆写
可覆写的操作符参考官网提供:
https://dart.dev/guides/language/language-tour#overridable-operators
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| e.g.
void main() { var person1 = new Person(20); var person2 = new Person(22);
print(person1 > person2); print(person1['age']); }
class Person { int age;
Person(this.age);
bool operator >(Person person) { return this.age > person.age; }
int operator [](String str) { if ('age' == str) { return age; }
return 0; } }
|