美文网首页
13.class继承

13.class继承

作者: web_jianshu | 来源:发表于2020-06-28 17:11 被阅读0次
<!DOCTYPE html>

<html lang="en">

  <head>

    <meta charset="UTF-8" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <meta http-equiv="X-UA-Compatible" content="ie=edge" />

    <title>Document</title>

  </head>

  <body></body>

</html>

<script>

  // 继承

  // 子类默认里面没有自己的this

  // 必须要调用super()才能够拿到this

  //   class Person {

  //     constructor(name, age) {

  //       this.name = name;

  //       this.age = age;

  //     }

  //     sayName() {

  //       console.log(this);

  //     }

  //   }

  //   // 继承 ES6中extends关键字实现相当于是ES5中直接替换原型对象

  //   class Teacher extends Person {

  //     // 继承Person类 Person作为父类  Teacher作为子类

  //     constructor(name, age, job) {

  //       super(name, age);

  //       // 调用父类构造器完成子类this对象塑造

  //       // let this = new Person(name, age);

  //       // this.__proto__ = Teacher.prototype

  //       // this.__proto__.__proto__ = Person.prototype

  //       this.job = job;

  //     }

  //     teach() {

  //       console.log(this.job); // 英语

  //     }

  //     static hobbits = "敲代码"; // 静态属性

  //     static show() {

  //       // 静态方法

  //       console.log(this.hobbits);

  //     }

  //   }

  //   let t1 = new Teacher("Jack", 19, "英语");

  //   console.log(t1);

  //   t1.sayName();

  //   t1.teach();

  // 子类必须在constructor方法中调用super方法,否则新建实例时会报错。这是因为子

  // 类自己的this对象,必须先通过父类的构造函数完成塑造,得到与父类同样的实例属

  // 性和方法,然后再对其进行加工,加上子类自己的实例属性和方法。

  // 如果不调用super方法,子类就得不到this对象。

  // ES5 的继承,实质是先创造子类的实例对象this,然后再将父类的方法添加到this

  // 上面(Parent.apply(this) )。ES6 的继承机制完全不同,实质是先将父类实例对象

  // 的属性和方法,加到this上面(所以必须先调用super方法),然后再用子类的构造函数

  // 修改this。

  // 在子类的构造函数中,只有调用super之后,才可以使用this关键字,否则会报错。这是因为

  // 子类实例的构建,基于父类实例,只有super方法才能调用父类实例。

  // 专题训练

  class China {

    constructor({ people }) {

      // new China()时默认执行 this = {__proto__: China.prototype} | Object.create(China.prototype)

      this.people = people;

      // 默认最后 return this

    }

    speak(language) {

      console.log(language);

    }

  }

  class Person extends China {

    constructor({ name, age, people }) {

      super({ people });

      // 相当于执行了以下三部

      // let this = new China({people}); // 得到父类China内部返回的this

      // this.__proto__ = Person.prototype

      // this.__proto__.__proto__ = China.prototype

      this.name = name;

      this.age = age;

      // 默认最后 return this

    }

    sayHI(db) {

      console.log(db + this.name);

    }

  }

  //   let p = new Person({ name: "jack", age: 19, people: "江西人" });

  //   console.log(p);

  //   p.sayHI("我叫");

  class Teacher extends Person {

    constructor({ name, age, job, people }) {

      super({ name, age, people });

      // 相当于执行了以下几步

      // let this = new Person({ name, age, people }) // 这个this是China内部返回到Person再返回到Teacher中 所以属于China

      // this.__proto__ = Teacher.prototype

      // this.__proto__.__proto__ = Person.prototype

      // this.__proto__.__proto__.__proto__ = China.prototype

      // this.__proto__.__proto__.__proto__.__proto__ = Object.prototype

      this.job = job;

      // 默认最后 return this

    }

    myReport(content) {

      console.log(content);

    }

  }

  let t = new Teacher({

    name: "沙雕",

    age: 26,

    job: "程序员",

    people: "黄种人"

  });

  console.log(t);

  t.speak("中文");

  t.myReport("在座各位都是辣鸡");

  t.sayHI("他的名字叫");

</script>

相关文章

  • 13.class继承

  • 继承 继承

    属性拷贝 继承不单单能通过原型链实现,也能通过其他方式实现,属性拷贝就是其中一种方法。 通过属性拷贝也能实现继承子...

  • 继承(单继承,多继承)

    将共性的内容放在父类中,子类只需要关注自己特有的内容 python中所有的内容都是对象,所有的对象都直接或间接继承...

  • js继承方式

    类式继承 构造函数继承 组合继承 类式继承 + 构造函数继承 原型式继承 寄生式继承 寄生组合式继承 寄生式继承 ...

  • Python-学习之路-08 OOP -02

    单继承和多继承 单继承:每个类只能继承一个类 多继承:每个类可以继承多个类 单继承的多继承的优缺点 菱形继承/钻石...

  • 原型相关(二)

    1.继承 继承方式:接口继承(只继承方法签名)实现继承(继承实际的方法)ECMAScript只支持实现继承,并且主...

  • 继承

    继承的引入和概述 继承案例和继承的好处 继承的弊端 Java中继承的特点 继承的注意实现和什么时候使用继承 继承中...

  • Java面向对象三大特性之继承

    继承 一、继承的特点 Java只支持单继承单继承 多继承 单继承、多继承优缺点①单继承优点:提高了代码的复用性,让...

  • 7、面向对象的程序设计3(《JS高级》笔记)

    三、继承 许多OO语言都支持两种继承方式:接口继承和实现继承。接口继承只继承方法签名,而实现继承则继承实际方法。由...

  • 【重学前端】JavaScript中的继承

    JavaScript中继承主要分为六种:类式继承(原型链继承)、构造函数继承、组合继承、原型式继承、寄生式继承、寄...

网友评论

      本文标题:13.class继承

      本文链接:https://www.haomeiwen.com/subject/dpmwfktx.html