标签:name new 接口 封装 private es6 子类 bsp xiaomi
public 完全开放
protected 对子类开放
private 对自己开放
// 父类,姓名,年龄可以公开,体重不能公开 class People { protected weight: any // 定义protected 属性 constructor (public name:any, public age: any) { this.name = name this.age = age this.weight = 120 } eat() { alert(`${this.name} eat something`) } speak() { alert(`my name is ${this.name}, age ${this.age}`) } } // 子类 class Student extends People { number private girlfriend // 定义private属性 constructor(name, age, number) { super(name, age); this.number = number; this.girlfriend = ‘xiaoli‘ } study() { alert(`${this.name} study`) } getWeight() { // 这个是在父类定义的,protected对自己,子类开放 alert(`${this.weight} 斤`) } } // 实例 let xaioming = new Student(‘xiaoming‘, 10, ‘A1‘); xaioming.getWeight(); // girlfriend是私有的,不能调用 //xiaoming.girlfriend;
标签:name new 接口 封装 private es6 子类 bsp xiaomi
原文地址:https://www.cnblogs.com/wzndkj/p/11706668.html