// 1.ES6 基础继承
// 定义一个父类
// class Dad{
// constructor(name,age){
// this.name = name;
// this.age = age;
// }
// hobby(){
// console.log("喜欢篮球");
// }
// }
// // 继承 : extends : 既可以继承 构造函数 又可以继承原型
// class Son extends Dad{
// }
// let son = new Son("张三",20);
// console.log(son);
// son.hobby();
// 2.ES6继承里的super
class Dad {
constructor(name, age) {
this.name = name;
this.age = age;
}
#getWigth() {
console.log("100kg");
}
hobby() {
console.log("喜欢篮球");
}
static fn() {
console.log("fn..");
}
}
// 继承 : extends : 既可以继承 构造函数 又可以继承原型
// ES6 继承的特性:
// 1.如果子类 没有constructor 自动继承父类属性和方法 且传参到父类
// 2.如果子类有constructor 方法 那么需要调取super方法完成继承;
// super : 1.super可以在子类的构造函数constructor里或者在子类函数里调用
// 2. super可以用于传参到父类
// 3.super 不能单独使用
// 4.在子类里必须在使用this之前调用 ;
class Son extends Dad {
constructor(name, age) {
super(name, age);
// console.log(super);
this.height = "178cm";
}
myhobby() {
console.log(this.name + "喜欢足球");
// super.hobby();
// this.#getWight();
}
}
Son.fn();
// extends 可以自动继承 父类的构造函数 也可以自动继承父类的原型,还可以自动继承 父类的静态成员; 私有不能继承,不能调用
let son = new Son("张三", 20);
// console.log(son)
son.myhobby();
// son.hobby();