File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -954,6 +954,48 @@ class Test extends getGreeterBase() {
954954
955955上面示例中,例一和例二的` extends ` 关键字后面都是构造函数,例三的` extends ` 关键字后面是一个表达式,执行后得到的也是一个构造函数。
956956
957+ ## override 关键字
958+
959+ 子类继承父类时,可以覆盖父类的同名方法。
960+
961+ ``` typescript
962+ class A {
963+ show() {
964+ // ...
965+ }
966+ hide() {
967+ // ...
968+ }
969+ }
970+ class B extends A {
971+ show() {
972+ // ...
973+ }
974+ hide() {
975+ // ...
976+ }
977+ }
978+ ```
979+
980+ 上面示例中,B 类定义了自己的` show() ` 方法和` hide() ` 方法,覆盖了 A 类的同名方法。
981+
982+ 但是有些时候,我们继承他人的类,可能会在不知不觉中,就覆盖了他人的方法。为了防止这种情况,TypeScript 4.3 引入了 [ override 关键字] ( https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-3.html#override-and-the---noimplicitoverride-flag ) 。
983+
984+ ``` typescript
985+ class B extends A {
986+ override show() {
987+ // ...
988+ }
989+ override hide() {
990+ // ...
991+ }
992+ }
993+ ```
994+
995+ 上面示例中,B 类的` show() ` 方法和` hide() ` 方法前面加了 override 关键字,明确表明作者的意图,就是要覆盖 A 类里面的这两个同名方法。这时,如果 A 类没有定义自己的` show() ` 方法和` hide() ` 方法,就会报错。
996+
997+ 但是,这依然没有解决,子类无意中覆盖父类同名方法的问题。因此,TypeScript 又提供了一个编译参数` noImplicitOverride ` 。一旦打开这个参数,子类覆盖父类的同名方法就会报错,除非使用了 override 关键字。
998+
957999## 可访问性修饰符
9581000
9591001类的内部成员的外部可访问性,由三个可访问性修饰符(access modifiers)控制:` public ` 、` private ` 和` protected ` 。
You can’t perform that action at this time.
0 commit comments