搜索
博主信息
博文 63
粉丝 8
评论 8
访问量 63719
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
PHP大牛成长之路:类的继承(原生+ES6)
周Sir-BLOG
原创
858人浏览过

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指针指向父类的原型方法,也就是调用方法时当本类找不到会逐级向上查找,也就是所谓的原型链(不知道理解是否正确?)
批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:js中的类的继承, 无论再怎么设计 , 与经典的其它语言相比, 还是有自己特点 的, 不要硬套其它语言的思路
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学