class Person {
name: string
static sex: string = '男'
public gy = '公有的'//默认是公有的,可以被任何地方访问
private sy = '私有的'//只能在当前类内部访问
protected bh: string = '保护的'//只能在当前类内部和子类内部访问
constructor(n: string) {
this.name = n
console.log('保护1', this.bh)
}
static setSex(): string {//static属性要和static方法配合使用
return this.sex
}
syFun(val: string) {
this.sy = val
return this.sy
}
}
class person extends Person {
constructor(n: string) {
super(n)
console.log(this.gy)
console.log('保护2', this.bh)
}
}
const p1 = new Person('欧文')
const p2 = new person('詹姆斯')
console.log(p1.syFun('哈哈哈'))
console.log(p1.name)
console.log(Person.setSex())
ts中class类讲解
最新推荐文章于 2024-05-23 16:18:55 发布