本节目标

  • 抽象 类、函数
  • 接口方式使用
  • 继承方式使用

环境

  • Dart 2.0.0

abstract 类、函数、成员

  • 普通类前加 abstract
1
2
3
4
5
6
abstract class Person {
static const String name = 'ducafecat';
void printName(){
print(name);
}
}

不能直接 new 实例化

1
2
var p = Person();
p.printName();

Dart 2 开始 new 可以不写,提高阅读体验

继承方式使用

定义

1
2
class Teacher extends Person {
}

实例

1
2
var user = Teacher();
user.printName();

接口方式使用

定义

1
2
3
4
5
6
7
8
9
10
11
abstract class Person {
static const String name = '';
void printName();
}

class Student implements Person {
String name = 'this is student';
void printName() {
print(name);
}
}

实例

1
2
var user = Student();
user.printName();

代码

参考


© 猫哥

https://ducafecat.tech