Skip to content

Commit c8aed9a

Browse files
committed
wip: class
1 parent 64cfc90 commit c8aed9a

File tree

2 files changed

+247
-14
lines changed

2 files changed

+247
-14
lines changed

CHANGELOG.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# 1.0.0 (2020-10-03)
1+
# 1.0.0 (2020-10-04)
22

33

44
### Bug Fixes
55

66
* add deploy folder, remove useless file with deploy.yml ([ae47a15](https://github.com/Rain120/typescript-guide/commit/ae47a15f31d84c749adeac9f4dcb8bd793860223))
77
* changelog before commit ([904d7cd](https://github.com/Rain120/typescript-guide/commit/904d7cd67335a1455aade81ae5638d0d85c16016))
8+
* deploy workflows error ([eb9efce](https://github.com/Rain120/typescript-guide/commit/eb9efcee47d42d8cfd3eae039dc904c71e73fdf1))
89
* deploy workflows error ([048dbe1](https://github.com/Rain120/typescript-guide/commit/048dbe1c70e0f0ecb914d8f44a14629938fac157))
910
* deploy workflows error ([6aa3dae](https://github.com/Rain120/typescript-guide/commit/6aa3dae982f03588a9b9f69b9f31dcdb67b0d14c))
1011
* deploy workflows error ([eeb7ab2](https://github.com/Rain120/typescript-guide/commit/eeb7ab2d0d7487b5a43249d0f0181083178961dd))
11-
* deploy workflows error ([eb9efce](https://github.com/Rain120/typescript-guide/commit/eb9efcee47d42d8cfd3eae039dc904c71e73fdf1))
1212
* Invalid workflow filed with deploy yml ([6c0d64a](https://github.com/Rain120/typescript-guide/commit/6c0d64a7dacbb38c8b34adcc4d74fbd56bb1e99c))
1313
* monaco editor build bug (cannot require async component) ([906f6e8](https://github.com/Rain120/typescript-guide/commit/906f6e8379efe762d76f16381b657e4752be4dd4))
1414
* monaco editor show normal modal and can edit ([da7a4c9](https://github.com/Rain120/typescript-guide/commit/da7a4c9316b6bf245ceccea93f3518ddaf810f80))
@@ -19,6 +19,7 @@
1919
* resolve advanced types path error ([8458a2c](https://github.com/Rain120/typescript-guide/commit/8458a2c6e2a22c53f387d2b53ba20f2c49e59d70))
2020
* the math logic description about intersection and union which at advanced types ([9bcc637](https://github.com/Rain120/typescript-guide/commit/9bcc637024767a71b0021c9a1f006fd3392e7ece))
2121
* update google analysis id ([2e1721b](https://github.com/Rain120/typescript-guide/commit/2e1721b7f5a7b671398f5d6ec2c094194c4229c0))
22+
* use commands install replace ci, because not package-lock.json ([64cfc90](https://github.com/Rain120/typescript-guide/commit/64cfc9003cb860bbdcb1855c971da4f1df1acc51))
2223

2324

2425
### Features
@@ -27,6 +28,7 @@
2728
* add tips, faqs folder; how to write docs md ([ff42474](https://github.com/Rain120/typescript-guide/commit/ff42474843622fcd2f6bb4ad13ea075934c039bc))
2829
* advanced types nullable; update union, assertion; others chores ([1b5936a](https://github.com/Rain120/typescript-guide/commit/1b5936aac1fe2c6363c90083231f66779b654a1e))
2930
* build in objects ([b62cd07](https://github.com/Rain120/typescript-guide/commit/b62cd07384f6423400d485bc3c4b68c6649c0ec2))
31+
* class ([78ff3cc](https://github.com/Rain120/typescript-guide/commit/78ff3ccf5e321f198777deb8fd20dc487add0bc0))
3032
* function ([5dba78c](https://github.com/Rain120/typescript-guide/commit/5dba78cd2eb6d18c70c4524ae328f4514c5eb1dd))
3133
* github action deploy workflows ([a98f275](https://github.com/Rain120/typescript-guide/commit/a98f27562f0cf7e3a82b46fa95fba8c1f2cd678b))
3234
* github action deploy workflows ([ae5128e](https://github.com/Rain120/typescript-guide/commit/ae5128e2dd7521698a0b5f11f783f48d41022e12))
@@ -39,11 +41,11 @@
3941
* refactor types(intersection, union, guard) to advanced types ([bde41ab](https://github.com/Rain120/typescript-guide/commit/bde41ab8e61ae59f21c907c7a1a86e932ee71f31))
4042
* ts basic type; update logo; guide; add editorconfig ([c0b6eb2](https://github.com/Rain120/typescript-guide/commit/c0b6eb272ab4d87706902cdda365230e507b860b))
4143
* ts operator; compile config; fix: playground frame width bug ([f2bcaa0](https://github.com/Rain120/typescript-guide/commit/f2bcaa05bfdf574037ec54ed3e651ce1d1353b5c))
44+
* **heading:** 加入特殊 heading 的处理; ignore IDE 相关的文件 ([5e9070b](https://github.com/Rain120/typescript-guide/commit/5e9070baf3b36cc0e753b9bde3a8aaec58590314))
4245
* type assertion ([f77f51c](https://github.com/Rain120/typescript-guide/commit/f77f51c574f036d6079742eca8f8affa7b06f617))
4346
* update the okrs about guide ([a762e9c](https://github.com/Rain120/typescript-guide/commit/a762e9cb156ac0bcdbdc64f0b477e41c49fb9e8d))
4447
* utility types; alias set bottom title; update generics params; ([a306a25](https://github.com/Rain120/typescript-guide/commit/a306a25c512246ef230a445ccb4e292eef8b8a7d))
4548
* vuepress docs template ([77f3649](https://github.com/Rain120/typescript-guide/commit/77f36499665edfad4f0333d6a55f54a541177f06))
46-
* **heading:** 加入特殊 heading 的处理; ignore IDE 相关的文件 ([5e9070b](https://github.com/Rain120/typescript-guide/commit/5e9070baf3b36cc0e753b9bde3a8aaec58590314))
4749
* wip generics, interface; add okr about the doc ([d2f6397](https://github.com/Rain120/typescript-guide/commit/d2f6397641ec4a590a51f297c1a9d61564500dff))
4850

4951

docs/zh/class/README.md

Lines changed: 242 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,33 @@
1212

1313
类的实例, 通过 `new`实例化
1414

15-
- 面向对象三大特性
15+
- [面向对象三大特性](#面向对象的三大特性)
1616

1717
👇👇👇 [Here](#面向对象的三大特性)
1818

19-
- 存取器
19+
- [存取器](#存取器)
2020

2121
`getter`: 对属性的取值行为
2222

2323
`setter`: 对属性的赋值行为
2424

25-
- 修饰器
25+
- [修饰符](#修饰符)
2626

2727
`public`: 修饰**公有**属性和方法 `(默认)`,可以在任何地方被访问到
2828

2929
`protected`: 修饰**保护**属性和方法,在子类中也是允许被访问的
3030

3131
`private`: 修饰**私有**属性和方法,不能在声明它的类的外部访问
3232

33-
- 抽象类
33+
- [静态属性 & 方法 `static`](#静态属性-方法)
34+
35+
**只能**通过类访问的属性 `or` 方法。
36+
37+
- [抽象类](#抽象类)
3438

3539
抽象类`(absctract)`是供其他类继承的基类,**抽象类不允许被实例化**。抽象类中的抽象方法**必须在子类中被实现**
3640

37-
- 接口
41+
- [接口](#把类当接口使用)
3842

3943
不同类之间公有的属性或方法,可以抽象成一个接口。接口可以被类实现 `(implements)`**一个类只能继承自另一个类,但是可以实现多个接口**
4044

@@ -46,22 +50,184 @@
4650

4751
### 继承
4852

49-
子类获取父类的所有属性和行为`(父类中用 private 修饰的变量和方法。)`
53+
子类获取父类的所有属性和行为`(除了父类中用 private 修饰的变量和方法)`
5054

5155
### 多态
5256

53-
同一操作作用于不同对象,可以有不同的解释,产生不同的执行结果
57+
**多种不同的实现方式**即为多态, 它允许方法重名,参数或返回值可以是父类型传入或返回
5458

5559
## 类的好处
5660

57-
对象提供了模型化和信息隐藏的好处。类提供了[可重复使用性](https://zh.wikipedia.org/w/index.php?title=可重複使用性&action=edit&redlink=1)的好处。自行车制造商一遍一遍地重用相同的蓝图来制造大量的自行车。软件程序员用相同的类,即相同的代码一遍一遍地创建对象。
61+
- 对象提供了模型化和信息隐藏的好处。
62+
63+
- 类提供了[可重复使用性](https://zh.wikipedia.org/w/index.php?title=可重複使用性&action=edit&redlink=1)的好处。
5864

5965
## 使用
6066

61-
#### 抽象类
67+
### public(公有) & protected(保护) & private(私有) 修饰符
68+
69+
#### public
70+
71+
修饰**公有**属性和方法 `(默认)`,可以在任何地方被访问到
6272

6373
```ts
64-
abstract class Boy {
74+
class Animal {
75+
public name: string;
76+
public constructor(theName: string) { this.name = theName; }
77+
public move(distanceInMeters: number) {
78+
console.log(`${this.name} moved ${distanceInMeters}m.`);
79+
}
80+
}
81+
```
82+
83+
[Playground](https://www.typescriptlang.org/play?#code/MYGwhgzhAECCB2BLAtmE0DeAoavoAcBXAIxEWGnjGQFMAuaCAFwCdF4BzAbhzyNPLRgAe3jMWhYE2EsAFEwAWNAHLV6jVuw4BKTNEWIIAOiq1oAXn1LVtLtAC+vXPzIVkwgG41ZAE0NMweGAaAEl4AFkaJhoWCAZ4QmRiGN1sPHShUQhhEBojEGEOWQADABIMA2NTGntody8faHK-ZkDgsMjo2PtkI2LtHnTHeyA)
84+
85+
#### protected
86+
87+
修饰**保护**属性和方法,在子类中也是允许被访问的
88+
89+
```ts
90+
class Person {
91+
protected name: string;
92+
constructor(name: string) { this.name = name; }
93+
}
94+
95+
class Employee extends Person {
96+
private department: string;
97+
98+
constructor(name: string, department: string) {
99+
super(name)
100+
this.department = department;
101+
}
102+
103+
public getElevatorPitch() {
104+
return `Hello, my name is ${this.name} and I work in ${this.department}.`;
105+
}
106+
}
107+
108+
let rainy = new Employee("Rain", "120");
109+
console.log(rainy.getElevatorPitch());
110+
console.log(rainy.name); // 错误
111+
```
112+
113+
[Playground](https://www.typescriptlang.org/play?#code/MYGwhgzhAEAKCmAnCB7AdtA3gKGn6ADoigC7zBkAm0aYAtvAFzQQmICWaA5gNy77B0rRAFcKKRAApaDZsM5cAlFmgkAFuwgA6GfGgBeGvXg9oAX2wXsoSDACidAiBQBPeHvgAPMmkowEyOhY-HhE7ABuYGTQlPAEYIgkDGgkcmwKfCHQgmjCYiQS0sZpHNwANDFxCUnwKSUKyjj4zSwiBEhFDIpZzeqaWrHxickkBpVDNSl8zVbNBCIARiDswNBc8CR2IPCRBYiw7CTAapKNPfiIGyKIGAAGABLwIM4VdC5GDNCa0AAkmH3aXRmaBgXzQACS0AA7hIANZfDB-AEDKrDWokMxaW7TfAWKzbUaIMCcd6GNDwKHQBxOVzuSQAIgASsS0PSKvSAIwAJgADPTFHwcqhtlpnFxJESSVp1pttrsJAcjidFALrEIUCKxRKWS4dMYBdAAPSG6CATFTAPfRQA)
114+
115+
#### private
116+
117+
修饰**私有**属性和方法,不能在声明它的类的外部访问
118+
119+
```ts
120+
class Animal {
121+
private name: string;
122+
constructor(theName: string) { this.name = theName; }
123+
}
124+
125+
class Cat extends Animal {
126+
constructor() { super("Cat"); }
127+
}
128+
129+
class Employee {
130+
private name: string;
131+
constructor(theName: string) { this.name = theName; }
132+
}
133+
134+
let animal = new Animal("Dog");
135+
let cat = new Cat();
136+
let employee = new Employee("Rain120");
137+
138+
animal = cat;
139+
animal = employee; // 错误: Animal 与 Employee 不兼容.
140+
```
141+
142+
[Playground](https://www.typescriptlang.org/play?#code/MYGwhgzhAECCB2BLAtmE0DeAoavoAcAnRANzABcBTaeMZSgLmgnOPgHMBuHPYAe3gtCAV2Dk+hABTkAFpQBydRs1aIOASkzRZiCADpa9aAF5tcxfU7QAvlltZQkGAGEK0SgA8q8ACYwEKGiYPLj8gqyi4lKaGMzC+JRSAESu5EnqVrb2jlDQAKLI+CB8AJ6U1Nh4BMRkVDRKTEJqXCHQYUKREtLmDSps7DFmugZKJmYKSpl2WFgglOTQYEio6KbwlADucMtokkkAInzs6dxzC8Bua5vQqZIZs-PuhcVl1FdbBUWl5XsASmBqACMACYAAwnGZLQKrNoUbhQlZjSjPb6UKwAenR0EAmKmAe+imAFEYA4OXyKNe0EAsHKAH0VAJ3aeiAA)
143+
144+
### readonly 修饰符
145+
146+
#### 属性
147+
148+
将属性设置为只读的。 **只读属性必须在声明时或构造函数里被初始化**
149+
150+
```ts
151+
class Octopus {
152+
readonly name: string;
153+
readonly numberOfLegs: number = 8;
154+
constructor (theName: string) {
155+
this.name = theName;
156+
}
157+
}
158+
let dad = new Octopus("Man with the 8 strong legs");
159+
dad.name = "Man with the 3-piece suit"; // 错误! name 是只读的.
160+
```
161+
162+
[Playground](https://www.typescriptlang.org/play?#code/MYGwhgzhAEDywBcD2AHArjA3gKGn6ATgKZgAmSAdiAJ7QVgC2RAXNBAgQJYUDmA3LnzEylGnTQMARkQKwAZgBkiPCKwoTpBaAF5oADgH5owSuwJpESLQAoEACyIA5RizYduPAJTQcRo-c4IADp6Jh1oeycXQ3wAX2x4kCIEaFIycIoiAHc4S3QIawAiAFkwCmgsznsIh303AkoeaCSVQs8BNNIQl3CSsoqquxqiaABmAFoUTiJgEYg0KsK+IA)
163+
164+
#### 参数
165+
166+
```ts
167+
class Octopus {
168+
readonly numberOfLegs: number = 8;
169+
constructor(readonly name: string) {
170+
}
171+
}
172+
```
173+
174+
[Playground](https://www.typescriptlang.org/play?#code/MYGwhgzhAEDywBcD2AHArjA3gKGn6ATgKZgAmSAdiAJ7QVoC2ARkQbAGYAyRA5hAFx1GLAtAC80ABwBuXPmCUICAmkRICACmJlKNOmAZFBSggEsKPAJTQc+aAF9s9oA)
175+
176+
### 存取器
177+
178+
```ts
179+
class Employee {
180+
private _fullName: string = '';
181+
182+
constructor(_fullName: string) {}
183+
184+
get fullName(): string {
185+
return this._fullName;
186+
}
187+
188+
set fullName(newName: string) {
189+
this._fullName = newName;
190+
}
191+
}
192+
193+
let employee = new Employee('Rainy');
194+
employee.fullName = "Rain120";
195+
console.log(employee.fullName)
196+
```
197+
198+
[Playground](https://www.typescriptlang.org/play?#code/MYGwhgzhAECiC2AHEB7AngUw9A3gKGkOkQCcBLANzABdsB9AMwFcQQA5MeDALmgmvIA7AObQAvNADkkgNx48ASGApB-Ek2DUUJABSMW7Tjz4CyIgJS4AvvKLRhGatGasOXHed5qzo-HbskjkwkgtDUABZkEAB0+q5GcnY2BEQQjs4Gbhg6ghgA7llepha4Kf5hkTFxhlzi0LkFCWXQNskg6RhIqJjYEg1wXehYOpIASmBmaJLmcp3IQxjRLjW90ABE42YAjABMAAxrcsqqKO3RqMI6c91YS5lG5kA)
199+
200+
**Note:**
201+
202+
- 存取器要求你将编译器设置为输出 `ECMAScript 5`或更高。 不支持降级到`ECMAScript 3`
203+
- **只带有 `get`不带有 `set`的存取器**自动被推断为 `readonly`。 这在从代码生成 `.d.ts`文件时是有帮助的,因为利用这个属性的用户会看到不允许够改变它的值。
204+
205+
### 静态属性 & 方法
206+
207+
只能通过类来访问,实例不行
208+
209+
```ts
210+
class Boy {
211+
static handsome: boolean = true;
212+
constructor(public name: string) {}
213+
static isHandsome() {
214+
return this.handsome;
215+
}
216+
}
217+
218+
let rain120 = new Boy('Rain120');
219+
console.log(Boy.handsome);
220+
console.log(Boy.isHandsome);
221+
console.log(rain120.handsome); // Property 'handsome' is a static member of type 'Boy'
222+
console.log(rain120.isHandsome); // Property 'isHandsome' is a static member of type 'Boy'
223+
```
224+
225+
[Playground](https://www.typescriptlang.org/play?#code/MYGwhgzhAEBCD2BPaBvAUNaEAuZsEthoALMAOwBMJ4BbAUwC5oAjeeEO86AXmmwCcArnQDcGaMHhkcQ4Nnj8AFAAdBzEIWhkw9JjPxkA5gEpUAX3E48m-BAAS5KrTqLT6TJn51sg-mT7EtgB0pJTU9GKYFhZoHNjQ-GAGAIwATAAMPFp0AO5wSIoA5ABKSWRp6YXGYpLS7HRBIPCGigiIIY7hdMZotdQcjc2tSEG2DmHOPX31gy2JKRkdE-RTUv0NTXNlFaP2nZNAA)
226+
227+
### 继承
228+
229+
```ts
230+
class Boy {
65231
handsome: boolean;
66232
name: string;
67233

@@ -70,7 +236,7 @@ abstract class Boy {
70236
this.handsome = handsome;
71237
}
72238

73-
abstract isHandsome(): boolean {}
239+
isHandsome() {}
74240
}
75241

76242
class Mine extends Boy {
@@ -83,7 +249,72 @@ const mine = new Mine('Rain120', true);
83249
console.log(mine)
84250
```
85251

252+
[Playground](https://www.typescriptlang.org/play?#code/MYGwhgzhAEBCD2BPaBvAUNaALMA7AJhPALYCmAXNAEbzwil4DcG0uYZlEALgE4CWuAObNMLYPFzceAV2Bd4PABRsO0KQMEAabHkIkK1WvTwBKVC0xcsfCADoVpaAF5W7UiMzQrN2zgJEyZx1-fQ8AXxY0AEgbAAldANJFMxQIiLRQSBgAWQFHUgAPLlJ-OCRzTDiE-WSKzx5SLmkeXC9rOz89MnC0dPFJLmhiPKDcUgB3aFyxxQByACUwAQBGACYABlntXmlSE2Z+onpbEHhBRWGxkyA)
253+
254+
#### 多态
255+
256+
```ts
257+
export function trace(cities: any, prop?: string, enterBreakPoint?: boolean): void;
258+
259+
export function trace(cities: any, enterBreakPoint?: boolean): void;
260+
261+
export function trace(enterBreakPoint?: boolean): void;
262+
263+
export function trace(...args: any[]): void {
264+
}
265+
```
266+
267+
[Playground](https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBAMwK4DsDGMCWEVxlAQzWAAo1MtgBnALjgJQE8AaOMKCMAfjqv0xQBzVsBQxgUAEJRgBANYAFCAJg84AIwgQANrJQBKOgDdlAEwDcAKEuhIsRKgzZc+IqXKVa9JiLETpsorKYmqaOnqGcCaYFta20PDI6Fg4eITEJKLiUjLySiqhWroMkdGxNuAJDsnOaW4kAHRNBFCCXgyMANoAuqVmcADelgC+QA)
268+
269+
### 抽象类
270+
271+
```ts
272+
abstract class Boy {
273+
handsome: boolean;
274+
name: string;
275+
276+
constructor(name: string, handsome: boolean) {
277+
this.name = name;
278+
this.handsome = handsome;
279+
}
280+
281+
abstract isHandsome(): boolean;
282+
}
283+
284+
class Mine extends Boy {
285+
isHandsome() {
286+
return this.handsome;
287+
}
288+
}
289+
290+
const mine = new Mine('Rain120', true);
291+
console.log(mine);
292+
```
293+
294+
[Playground](https://www.typescriptlang.org/play?#code/IYIwzgLgTsDGEAJYBthjAgQgewJ4IG8AoBBAC2ADsATMbAWwFMAuBEbbZRqgbhIUrAmrSFACWlAOZ9S-WNkqiArvGxQAFIOEJREyQBpyVWgxZsOXKgEpC-UhDJiwAOi2MEAXgFDGM0ggcnZwoaOiZPI1DTPwBffn5QUThEJwAJYzDGdStWdk5uSj44ohQ0DABZCXdGAA8IRlCsPFtSNIzTbJb-KEYIJShKAMcXEJMmWKJi+UVEeiqIykYAdwRKxfUAcgAlYAkARgAmAAYNw2glRis+abouZ2RsSXU5xaugA)
295+
296+
### 把类当接口使用
297+
298+
```ts
299+
class Boy {
300+
handsome: boolean = true;
301+
}
302+
303+
class Girl {
304+
beatiful: boolean = true;
305+
}
306+
307+
enum Gender {
308+
'MALE',
309+
'FEMALE'
310+
}
311+
312+
interface Person extends Boy, Girl {
313+
gender: Gender;
314+
}
315+
```
86316

317+
[Playground](https://www.typescriptlang.org/play?#code/MYGwhgzhAEBCD2BPaBvAUNaALMA7AJhPALYCmAXNAEbzwil7QC80ALgE4CupA3GgL5o0oSDADiAS3YhUGag1YSAZpxCUadBrmZsuvAUNK5OxaGKP5S7WZgDkAWQCCAGQCitgDRzbAMVdO3WwM0CVxWKyUwYFJoAAUrIm1SAA9wghgERA8zKRl0TABzCytKcwIrPn4gA)
87318

88319
## 快来耍耍啊
89320

0 commit comments

Comments
 (0)