Skip to content

Commit 78ff3cc

Browse files
committed
feat: class
1 parent cdb21f2 commit 78ff3cc

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1.0.0 (2020-09-21)
1+
# 1.0.0 (2020-10-03)
22

33

44
### Bug Fixes

docs/zh/class/README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,90 @@
11
## 定义 <Badge text='WIP' type='warning' />
22

3+
类(英语: `class`)在[**面向对象编程(oop)**](https://zh.wikipedia.org/zh-cn/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1)中是一种面向对象计算机编程语言的构造, 是创建对象的蓝图, 描述了所创建的对象共同的属性和方法。
4+
5+
`JavaScript` 语言中, 生成实例对象的传统方法是通过构造函数。`ES6` 提供了更接近传统语言的写法, 引入了 `Class(类)`这个概念, 作为对象的模板。通过 `class` 关键字, 可以定义类。
6+
7+
`TypeScript` 除了实现了所有 `ES6` 中的类的功能以外,还添加了一些新的用法。
8+
9+
### 概念
10+
11+
- 对象
12+
13+
类的实例, 通过 `new`实例化
14+
15+
- 面向对象三大特性
16+
17+
👇👇👇 [Here](#面向对象的三大特性)
18+
19+
- 存取器
20+
21+
`getter`: 对属性的取值行为
22+
23+
`setter`: 对属性的赋值行为
24+
25+
- 修饰器
26+
27+
`public`: 修饰**公有**属性和方法 `(默认)`,可以在任何地方被访问到
28+
29+
`protected`: 修饰**保护**属性和方法,在子类中也是允许被访问的
30+
31+
`private`: 修饰**私有**属性和方法,不能在声明它的类的外部访问
32+
33+
- 抽象类
34+
35+
抽象类`(absctract)`是供其他类继承的基类,**抽象类不允许被实例化**。抽象类中的抽象方法**必须在子类中被实现**
36+
37+
- 接口
38+
39+
不同类之间公有的属性或方法,可以抽象成一个接口。接口可以被类实现 `(implements)`**一个类只能继承自另一个类,但是可以实现多个接口**
40+
41+
## 面向对象的三大特性
42+
43+
### 封装
44+
45+
利用抽象数据类型将数据和基于数据的操作封装在一起,使其构成一个不可分割的独立实体,**数据被保护在抽象数据类型的内部,尽可能地隐藏内部的细节**,只保留一些对外接口使之与外部发生联系。
46+
47+
### 继承
48+
49+
子类获取父类的所有属性和行为`(父类中用 private 修饰的变量和方法。)`
50+
51+
### 多态
52+
53+
同一操作作用于不同对象,可以有不同的解释,产生不同的执行结果。
54+
55+
## 类的好处
56+
57+
对象提供了模型化和信息隐藏的好处。类提供了[可重复使用性](https://zh.wikipedia.org/w/index.php?title=可重複使用性&action=edit&redlink=1)的好处。自行车制造商一遍一遍地重用相同的蓝图来制造大量的自行车。软件程序员用相同的类,即相同的代码一遍一遍地创建对象。
58+
359
## 使用
460

61+
#### 抽象类
62+
63+
```ts
64+
abstract class Boy {
65+
handsome: boolean;
66+
name: string;
67+
68+
constructor(name: string, handsome: boolean) {
69+
this.name = name;
70+
this.handsome = handsome;
71+
}
72+
73+
abstract isHandsome(): boolean {}
74+
}
75+
76+
class Mine extends Boy {
77+
isHandsome() {
78+
return this.handsome;
79+
}
80+
}
81+
82+
const mine = new Mine('Rain120', true);
83+
console.log(mine)
84+
```
85+
86+
87+
588
## 快来耍耍啊
689

790
### 🌰🌰
@@ -29,3 +112,9 @@
29112
## 参考资料
30113

31114
[handbook - classes](https://www.typescriptlang.org/docs/handbook/classes.html)
115+
116+
[深入理解 TypeScript - 类](https://jkchao.github.io/typescript-book-chinese/faqs/class.html)
117+
118+
[ECMAScript 6入门 - Class基本语法](https://es6.ruanyifeng.com/#docs/class)
119+
120+
[类 (计算机科学)](https://zh.wikipedia.org/zh-cn/%E7%B1%BB_(%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%A7%91%E5%AD%A6))

0 commit comments

Comments
 (0)