1.父类,人类
function People(name, age, sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
People.prototype.sayHello = function() {
console.log('你好,我是' + this.name + `我今年${this.age}岁了`);
};
People.prototype.sleep = function() {
console.log(this.name + '开始睡觉,zzzzzz');
};
2.子类,学生类
function Student(name, age, sex, scholl, studentNumber) {
this.name = name
this.age = age
this.sex = sex
this.scholl = scholl
this.studentNumber = studentNumber
}
一行语句实现继承:子类的protoype指向新的new出来的父类的实例
Student.prototype = new People();
Student.prototype.study = function() {
console.log(this.name + '正在学习');
}
Student.prototype.exam = function() {
console.log(this.name + '正在考试');
}
3.实例化
var xiexiaobai = new Student('谢小白', 9, '女', '新兴中学', 259011)
4.xiexiaobai 实例化后的子类能调用父类方法
xiexiaobai.study();
xiexiaobai.exam();
xiexiaobai.sayHello();
xiexiaobai.sleep();