Typescript中的类修饰符
程序员文章站
2022-07-03 23:17:29
...
// 类里面的修饰符(不写的话,默认public)
* public 共有,在当前类,子类,类外面都可以访问;
* protected: 保护类型,在当前类,子类可以访问,类外边不可访问;
* private:私有类型,在当前类可以访问,子类,类外边不可访问;
class Person {
public name: string;
protected age: number;
private addr: string;
constructor(name: string,age:number, addr:string) {
this.name = name;
this.age = age;
this.addr = addr;
}
run(): string {
return `${this.age}岁的${this.name}在${this.addr}运动`;
}
}
class Web extends Person {
constructor(name: string, age: number, addr: string) {
super(name,age,addr)
}
work(): void {
console.log(`${this.age}岁的${this.name}在运动`);
}
}
var w = new Web('张飞',32,'蜀国');
console.log(w.run());
w.work();
上一篇: div拖动改变大小 (横向)
下一篇: js 可拖动div 调整大小