美文网首页
类和继承

类和继承

作者: 饥人谷_若愚 | 来源:发表于2017-11-19 09:31 被阅读29次
  1. 构造函数
class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  sayHello() {
    console.log( `hello, ${this.name}, i am ${this.age} years old`);
  }
}

等价于

function Person(name, age) {
  this.name = name;
  this.age = age;
}

Person.prototype.sayHello = function () {
  console.log(  `hello, ${this.name}, i am ${this.age} years old`);
};

var p = new Person('hunger', 2);
  1. 静态方法
class EventCenter {
  static fire() {
    return 'fire';
  }
  static on(){
    return 'on'
  }
}

等同于

function EventCenter(){
}
EventCenter.fire = function(){}
EventCenter.on = function(){}

  1. 继承
class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  sayHello() {
    console.log( `hello, ${this.name}, i am ${this.age} years old`);
  }
}
class Student extends Person {
  constructor(name, age, score) {
    super(name, age); 
    this.score = score;
  }

  sayScore() {
     console.log(  `hello, ${this.name}, i am ${this.age} years old, i get ${this.score}`);
  }
}

相关文章

  • python零基础13:类的继承和定制

    类的定制和继承是什么? 类的继承 类的定制 类的继承要怎么写? 继承基础语法 继承之多层继承 继承的多重继承 类的...

  • [C++之旅] 18 公有继承、保护继承和私有继承

    [C++之旅] 18 公有继承、保护继承和私有继承 继承 继承的类称为派生类或子类,被继承的类称为基类或父类。继承...

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

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

  • 类和继承

    生成实例对象的传统方法是通过构造函数 ES6 的class造函数 静态方法 类相当于实例的原型,所有在类中定义的方...

  • 类和继承

    类 Kotlin 中使用 关键字 Class 声明类 构造函数 Kotlin 中类可以有主构造函数 和 次构造函数...

  • 类和继承

    构造函数 等价于 静态方法 等同于 继承

  • 类和继承

    1.构造函数 等价于 2. 静态方法 等同于 3.继承

  • Swift教程之继承

    继承 类可以从另一个类继承方法、属性和其他特性,继承类称为子类,被继承类为其超类。Swift的类可以调用和访问超类...

  • Inheritances(继承)

    //感觉继承和oc的继承差不多 //继承// “一个类可以继承另一个类的方法,属性和其它特性。当一个类继承其它类时...

  • Swift - 继承

    继承 一个类可以继承另一个类的方法,属性和其它特性。当一个类继承其它类时,继承类叫子类,被继承类叫超类(或父类)。...

网友评论

      本文标题:类和继承

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