实现继承的时候,子类是必须有父类的所有属性和方法的,但子类的属性是手动添加上的,父类的方法不用手动添加到子类的方法吗?
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script> // 父类 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 + '开始睡觉'); }; // 子类 function Student(name, age, sex, school, id) { this.name = name; this.age = age; this.sex = sex; this.school = school; this.id = id; }; // 实现继承 Student.prototype = new People(); Student.prototype.study = function () { console.log('我正在学习'); }; Student.prototype.exam = function () { console.log('我正在考试'); }; var hanmeimei = new Student('韩梅梅', 20, '女'); hanmeimei.sayHello(); hanmeimei.sleep(); </script> </body> </html>
18
收起
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星