class Person{
static tips(){
return 'hanamaru ka wa yii'
}
static kis(){
// 这里的this绑定类本身,不绑定实例
console.log(this.tips());
}
constructor(name,age){
this.name=name
this.age=age
}
show(){
console.log(this.name);
console.log(this.age);
}
// 用setter来设置类的功能
set other(val){
this.value=val
}
get other(){
return `这是set的值:${this.value}`
}
}
const user=new Person('xcw',8)
user.show()
user.other='HUAWAN'
console.log(user.other);
Person.kis()//用类调用静态方法来输出静态的内容
// 继承
class Person_son extends Person{
constructor(name,age,gender){
// 在子类使用constructor必须使用this之前来使用super来初始化this
super(name,age)
this.gender=gender
}
show(){
console.log(this.name);
console.log(this.age);
console.log(this.gender);
}
}
const user_son=new Person_son('hana',16,'girl')
user_son.show()
结果
xcw
8
这是set的值:HUAWAN
hanamaru ka wa yii
hana
16
girl