class类中的访问类型(public 、private、和protected)
public
- 从英文字面的解释就是公共的或者说是公众的,在程序里的意思就是允许在类的内部和外部被调用
- 如果在class中不给访问属性作任何定义,那么默认的就是public访问属性
class person {
name:string
sayHi(){
return this.name +"你好!"
}
}
let Person = new person()
Person.name = "小哈"
console.log(Person.name)
console.log(Person.sayHi())
private
class person {
private content="我是人类"
private sayHi(){
return this.content
}
}
let Person = new person()
Person.content = "aaa"
protected
class person {
protected content = "我是人类"
sayHi(){
return this.content
}
}
let Person = new person()
class xiaoHa extends person{
public sayHi(){
return this.content
}
}
let XH= new xiaoHa()
console.log(XH.sayHi())