| 修饰符 |
类内部 |
类外部 |
子类 |
| public |
可以访问类中的属性 |
可以访问类中的属性 |
可以访问类中的属性 |
| private |
可以访问类中的属性 |
不可以访问类中的属性 |
不可以访问类中的属性 |
| protected |
可以访问类中的属性 |
不可以访问类中的属性 |
可以访问类中的属性 |
//protectedclass Student2{ protected name:string = "lisi" sayName(){ console.log(this.name) }}var s2:Student2 = new Student2() console.log(s2.name)//此行会报错
#protected和private的区别class Student2{ protected name:string = "lisi" sayName(){ console.log(this.name) }}// protected和private的区别:在子类中/* protected修饰的属性,在子类中可以访问 private修饰的属性,在子类中不可以访问*/class MidStudent extends Student2{ getName(){ console.log(this.name); }}var m:MidStudent = new MidStudent()m.getName()