类与枚举
- 类(Class): 定义了一切事物的抽象特点
- 对象(Object): 类的实例
- 面向对象(OOP)三大特征:封装、继承、多态
js 中的类
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 |
|
ts 中的类
- Public: 修饰的属性或方法是共有的
- Private: 修饰的属性或方法是私有的
- Protected: 修饰的属性或方法是受保护的
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 42 43 44 45 |
|
类和接口
- 继承的困境
- 类可以使用 implements 来实现接口
1 2 3 4 5 6 7 8 9 10 11 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
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 |
|
枚举
1 2 3 4 5 6 7 8 |
|
1 2 3 4 5 6 7 8 |
|
查看编译出的代码:
1 2 3 4 5 6 7 8 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
常量枚举
1 2 3 4 5 6 7 8 9 10 11 |
|
编译后的内容:
1 2 3 4 |
|