File tree Expand file tree Collapse file tree 1 file changed +31
-1
lines changed Expand file tree Collapse file tree 1 file changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -265,7 +265,7 @@ let a = new Animal('Jack');
265
265
``` ts
266
266
class Animal {
267
267
public name;
268
- private constructor (name ) {
268
+ protected constructor (name ) {
269
269
this .name = name ;
270
270
}
271
271
}
@@ -291,6 +291,36 @@ class Animal {
291
291
}
292
292
```
293
293
294
+ ### readonly
295
+
296
+ 只读属性关键字,只允许出现在属性声明或索引签名中。
297
+
298
+ ``` ts
299
+ class Animal {
300
+ readonly name;
301
+ public constructor (name ) {
302
+ this .name = name ;
303
+ }
304
+ }
305
+
306
+ let a = new Animal (' Jack' );
307
+ console .log (a .name ); // Jack
308
+ a .name = ' Tom' ;
309
+
310
+ // index.ts(10,3): TS2540: Cannot assign to 'name' because it is a read-only property.
311
+ ```
312
+
313
+ 注意如果 ` readonly ` 和其他访问修饰符同时存在的话,需要写在其后面。
314
+
315
+ ``` ts
316
+ class Animal {
317
+ // public readonly name;
318
+ public constructor (public readonly name ) {
319
+ this .name = name ;
320
+ }
321
+ }
322
+ ```
323
+
294
324
### 抽象类
295
325
296
326
` abstract ` 用于定义抽象类和其中的抽象方法。
You can’t perform that action at this time.
0 commit comments