// public 定义类的变量默认就是公共的,继承的子类可以通过this来访问
// private 定义类的私有属性,只能在内部访问
class Person {
private name: string = ""
getName() {
return this.name
}
setName(newName) {
this.name = newName
}
}
const p = new Person()
console.log(p.getName())
p.setName('xbj')
// protected: 在类的内部和子类中可以访问,在外面就访问不到了
class Person {
protected name: string
constructor(name: string) {
this.name = name
}
}
class Student extends Person {
constructor(name: string, age: number){
super(name)
}
getName() {
return this.name
}
}