ts类里面的静态方法 静态属性的使用
dearweb
发布:2021-07-25 13:04:05阅读:
ts类里面的静态方法的使用
class Person {
public name:string
constructor(name:string){
this.name=name
}
goRun(){ // 实例方法
console.log(`${this.name}去运动`);
}
goWork(){// 实例方法
console.log(`${this.name}去工作`);
}
// 加了static 就为静态方法
static goWorkStatic(){
console.log(`静态方法`)
}
}
let P = new Person('王五')
P.goRun()
// 调用静态方法 直接调用
Person.goWorkStatic()ts类里面的静态属性
// 定义静态属性
class Person {
public name:string
static sex = '女'
constructor(name:string){
this.name=name
}
}
// 调用静态方法 直接调用
console.log(Person.sex);小礼物走一波,支持作者
赏还没有人赞赏,支持一波吧