class Person {
// 静态属性
static age = 18;
// 私有属性
#count = 0;
constructor(name,food) {
this.name = name
this.food = food
}
// 静态方法
static say(){
console.log('say');
}
// 私有方法
#playGanme() {
console.log('Animal-playGanme');
}
eat() {
console.log('吃'+this.food );
}
}
class Student extends Person{
constructor(name,age,food) {
super(name,food)
this.age = age
}
study() {
console.log(this.name+this.age+'学习', );
}
}
let st = new Student('我',12,'苹果')
st.study()
st.eat()
//静态属性和静态方法 实例获取不到,子类可以继承
st.age undefined
st.say() //报错
//私有属性和私有方法 只能在自己的类里使用
JS ES6类和继承
于 2022-03-04 11:32:01 首次发布