Blogger Information
Blog 63
fans 8
comment 8
visits 50027
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP大牛成长之路:类的继承(原生+ES6)
周Sir-BLOG
Original
685 people have browsed it

1、类的继承(子类不重写父类方法)

1.1、js原生类的继承

  1. //父类
  2. function Father(name, email, age) {
  3. this.name = name;
  4. this.email = email;
  5. this.age = age;
  6. }
  7. //父类添加公共/原型方法
  8. Father.prototype.getUser = function () {
  9. return `我是${this.name},今年${this.age}岁,邮箱是:${this.email}`;
  10. };
  11. function Son(name, email, age) {
  12. Father.call(this, name, email, age);
  13. }
  14. // 更新当前子类的原型对象,这样才能实现真正意义上继承
  15. Son.prototype = Object.create(Father.prototype);
  16. // 手工补上constructor
  17. Son.prototype.constructor = Son;
  18. // 实例化子类:
  19. const son = new Son("peter", "peter@php.cn", 18);
  20. // 使用子类访问getUser方法
  21. console.log(son.getUser());
  22. // 输出:我是peter,今年18岁,邮箱是:peter@php.cn

1.2、ES6类的继承

  1. //父类
  2. class Father {
  3. //构造方法
  4. constructor(name, email, age) {
  5. this.name = name;
  6. this.email = email;
  7. this.age = age;
  8. }
  9. // 原型方法
  10. getUser() {
  11. return `我是${this.name},今年${this.age}岁,邮箱是:${this.email}`;
  12. }
  13. }
  14. //子类
  15. class Son extends Father {}
  16. // 实例化子类:
  17. const son = new Son("peter", "peter@php.cn", 18);
  18. // 使用子类访问getUser方法
  19. console.log(son.getUser());
  20. // 输出:我是peter,今年18岁,邮箱是:peter@php.cn

2、类的继承(子类重写父类方法)

2.1、js原生类的继承

  1. //父类
  2. function Father(name, email, age) {
  3. this.name = name;
  4. this.email = email;
  5. this.age = age;
  6. }
  7. //父类添加公共/原型方法
  8. Father.prototype.getUser = function () {
  9. return `我是${this.name},今年${this.age}岁,邮箱是:${this.email}`;
  10. };
  11. //子类
  12. function Son(name, email, age, lecturer) {
  13. Father.call(this, name, email, age);
  14. this.lecturer = lecturer;
  15. }
  16. // 更新当前子类的原型对象,这样才能实现真正意义上继承
  17. Son.prototype = Object.create(Father.prototype);
  18. // 手工补上constructor
  19. Son.prototype.constructor = Son;
  20. // 子类中重写父类的原型方法
  21. Son.prototype.getUser = function () {
  22. // return `我是${this.name},今年${this.age}岁,邮箱是:${this.email},职业: ${this.lecturer}`;
  23. // .concat()连接多个字符串
  24. return Father.prototype.getUser
  25. .call(this)
  26. .concat(`,职业: ${this.lecturer}`);
  27. };
  28. // 实例化子类:
  29. const son = new Son("peter", "peter@php.cn", 18, "讲师");
  30. // 使用子类访问getUser方法
  31. console.log(son.getUser());
  32. // 输出:我是peter,今年18岁,邮箱是:peter@php.cn,职业: 讲师

2.2、ES6类的继承

  1. //父类
  2. class Father {
  3. //构造方法
  4. constructor(name, email, age) {
  5. this.name = name;
  6. this.email = email;
  7. this.age = age;
  8. }
  9. // 原型方法
  10. getUser() {
  11. return `我是${this.name},今年${this.age}岁,邮箱是:${this.email}`;
  12. }
  13. }
  14. //子类
  15. class Son extends Father {
  16. constructor(name, email, age, lecturer) {
  17. // 调用父类的构造方法
  18. // super()必须是第一条,否则不能正确的生成子类的this
  19. super(name, email, age);
  20. this.lecturer = lecturer;
  21. }
  22. // 子类中重写父类的原型方法
  23. getUser() {
  24. // return `我是${this.name},今年${this.age}岁,邮箱是:${this.email},职业: ${this.lecturer}`;
  25. // .concat()连接多个字符串
  26. return super.getUser().concat(`,职业: ${this.lecturer}`);
  27. }
  28. }
  29. // 实例化子类:
  30. const son = new Son("peter", "peter@php.cn", 18,"讲师");
  31. // 使用子类访问getUser方法
  32. console.log(son.getUser());
  33. // 输出:我是peter,今年18岁,邮箱是:peter@php.cn,职业: 讲师

总结:

  • 个人理解:类的继承不论是原生还是ES6写法,都是通过prototype属性将this指针指向父类的原型方法,也就是调用方法时当本类找不到会逐级向上查找,也就是所谓的原型链(不知道理解是否正确?)
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:js中的类的继承, 无论再怎么设计 , 与经典的其它语言相比, 还是有自己特点 的, 不要硬套其它语言的思路
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post