//使用class 关键字 ,定义类 =>(ES5中构造函数)
//定义一个父类 : 父类不会被用于实例化 , 只提供与继承使用
class Animal{
//构造函数 constructor 每个类中都会有这个内置的构造函数 ,会在类实例化的时候自动调用 ,用于初始化实例对象(在new 需要设置一些属性)
constructor(name,type,color){ //此处的参数 ,在new 的构造函数中传递过来的
console.log("Animal实例中...");
this.name = name;
this.type = type;
this.color = color;
}
run(){
console.log("我能跑...");
}
shout(){
console.log("我能叫...");
}
}
//定义一个子类 - Cat
class Cat extends Animal{
constructor(name,type,color){
super(name,type,color);//相当于调用了父类中的构造函数
console.log("Cat实例化中 ...");
}
catchMouse(){
console.log("我能抓老鼠!");
}
}
//定义一个子类 - Dog
class Dog extends Animal{
constructor(name,type,color){
super(name,type,color);//相当于调用了父类中的构造函数
console.log("Dog 实例化中....");
}
watchHouse(){
console.log("我能看家");
}
}
let cat = new Cat("小花","橘猫","橘黄色");
let dog = new Dog("阿黄","藏獒","橘红色");
console.log(cat.run === dog.run); // true 说明两个实例对象都继承了来自于同一个父类中的方法 (在实例对象的原型上)