dart 版本 > 3.x
🌙 1. extends(继承)、implements(实现)、 mixin-with(混入)
extends
关键字用于创建一个类的子类,子类将继承父类的所有公共属性和方法,可以访问并使用它们。子类可以通过重写父类的方法来实现多态性。
class Person {
final String name;
final int age;
Person(this.name, this.age);
void sayHello() {
print('Hello, my name is $name and I am $age years old.');
}
}
class Student extends Person {
final String school;
Student(super.name, super.age, this.school);
void study() {
print('$name is now studying.');
}
// 子类重写父类的方法
void sayHello() {
print(
'Hello, my name is $name and I am $age years old. I am a student at $school.');
}
}
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
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
implements
关键字用于创建一个类的多重实现。
abstract class Animal {
void eat();
}
abstract class Mammal implements Animal {
void sleep();
}
class Human implements Mammal {
void eat() {
print('I am eating.');
}
void sleep() {
print('I am sleeping.');
}
void work() {
print('I am working.');
}
}
void main() {
// error: Abstract classes can't be instantiated.
// var animal = Animal();
var human = Human();
human.eat();
human.sleep();
human.work();
}
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
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
mixin
关键字用于创建一个混入类,混入类将混入其他类的公共属性和方法,可以访问并使用它们。
mixin
一般和 with
一起使用, 将一个或多个 mixin
类的功能混入到一个类中。Mixin
是一种特殊的多重继承,允许在不继承类的情况下重用代码。
mixin Flyable {
void fly() {
print('I can fly.');
}
}
mixin Walkable {
void walk() {
print('I can walk.');
}
}
class Bird with Flyable, Walkable {
void sing() {
print('I can sing.');
}
void fly() {
print('I am flying.');
}
void walk() {
print('I am walking.');
}
}
void main() {
// error: Mixins can't be instantiated.
// var flyable = Flyable();
var bird = Bird();
bird.fly();
bird.walk();
bird.sing();
}
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
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
- interface 接口
interface
一般和 implements
一起使用,将一个或多个 interface
类的功能实现到一个类中。
// 此处interface可以去掉
interface class CustomInterface {
void customMethod() {}
}
class CustomClass implements CustomInterface {
void customMethod() {
// 实现接口中定义的方法
print('customMethod');
}
}
void main() {
CustomClass c = CustomClass();
c.customMethod();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
🌙 2.Class modifiers(类型修饰符) (opens new window)
修饰符 | 作用 | 说明 |
---|---|---|
abstract | 抽象类修饰符 | abstract 修饰符用于定义一个类,该类不需要完整实现其接口,并且不能被实例化。抽象类通常包含抽象方法,需要子类实现 |
base | 基类修饰符 | base 修饰符用于强制类或混入类的实现仅在同一库内部。它确保子类在同一库内继承基类的实现,并防止外部库破坏基类的保证。 |
final | 静态常量修饰符 | final 修饰符用于关闭类的类型层次结构,防止外部类继承或实现。这有助于确保 API 的稳定性和避免意外的子类化。 |
interface | 接口修饰符 | interface 修饰符用于定义一个接口,其他库可以实现该接口,但不能扩展它。这有助于确保在调用实例方法时始终调用同一库中的已知实现。 |
sealed | 密封可枚举的子类型 | sealed 修饰符用于创建一个已知的、可枚举的子类型集合,并确保在 switch 语句中详尽处理所有可能的子类型。它阻止类在当前库外部被扩展或实现。 |
mixin | 混入修饰符 | mixin 修饰符用于创建可混入到其他类中的可重用代码块。混入类可以为类提供额外的功能,而无需继承。 |
🌙 3.Valid combinations of class modifiers(类修饰符的有效组合) (opens new window)
Declaration 声明方式 | Construct? 可实例化? | Extend? 可被外部继承? | Implement? 可被外部实现? | Mix in? 可被混入? | Exhaustive? 可被穷举(switch case)? |
---|---|---|---|---|---|
class | Yes | Yes | Yes | No | No |
base class | Yes | Yes | No | No | No |
interface class | Yes | No | Yes | No | No |
final class | Yes | No | No | No | No |
sealed class | No | No | No | No | Yes |
abstract class | No | Yes | Yes | No | No |
abstract base class | No | Yes | No | No | No |
abstract interface class | No | No | Yes | No | No |
abstract final class | No | No | No | No | No |
mixin class | Yes | Yes | Yes | Yes | No |
base mixin class | Yes | Yes | No | Yes | No |
abstract mixin class | No | Yes | Yes | Yes | No |
abstract base mixin class | No | Yes | No | Yes | No |
mixin | No | No | Yes | Yes | No |
base mixin | No | No | No | Yes | No |
- 纯粹的
class
类可以被实例化、继承extend
、实现implement
、不能被混入mixin
(3.x 之后必须使用mixin
修饰才可以被mixin
) - 纯粹的
interface class
接口类可以被实现implement
, 但不可被外部继承extend
- 纯粹的
abstrcat class
抽象类不能被直接实例化, 可被继承extend
, 也可被实现implement
- 纯粹的
sealed class
类不允许被外部继承extends
或 实现implements
- 纯粹的
base class
修饰的类在外部 允许继承extend
,不允许实现implement
- 纯粹的
final class
不允许被直接继承extend
, 子类被base
、final
或sealed
修饰的类继承extend
,不允许实现implement