美文网首页
类 继承

类 继承

作者: RickyWu585 | 来源:发表于2021-05-30 14:50 被阅读0次

构造函数

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(){
  console.log(` hello , ${this.name}, i am ${this.age} years old`)
}
var p = new Person('frank',18)

静态方法

class EventCenter{
  static fire(){
    return 'fire'
  }
  static on(){
    return 'on'
  }
}

相当于

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

继承

  • class实现继承
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(`my score is ${this.score}`)
  }
}
  • 原型实现继承
function Person(name,age){
  this.name = name
  this.age = age
}
Person.prototype.sayHello(){
  console.log('hello world')
}
function Student(name,age,score){
  Person.apply(this,arguments)
  this.score = score
}
function temp(){}
temp.prototype = Person.prototype
Student.prototype = new temp()
Student.prototype.contructor = Student
Student.prototype.sayScore = function(){console.log(`i get ${this.score}`)}

var stu = new Student('frank',18,100) 

相关文章

  • 一阶段day16-01面向对象

    Python中类支持继承,并且支持多继承 一、继承 1、什么是继承 父类(超类):被继承的类子类:继承父类的类继承...

  • 2018-10-19面向对象和pygame

    类的继承 python中的类支持继承,并且支持多继承 1.什么是继承 父类(超类):被继承的类子类:继承的类继承就...

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

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

  • Day16总结:面向对象和pygame

    类的继承 python中类支持继承,并且支持多继承 1.什么是继承 父类(超类):被继承的类子类:去继承父类的类继...

  • python 面向对象和pygame

    一、类的继承 python中类支持继承,并且支持多继承 1.什么是继承父类(超类):被继承的类子类:去继承父类的类...

  • 10.19 day16面向对象和pygame

    1.类的继承python中类 支持继承,并且支持多继承()1.什么是继承父类(超类):被继承的类 子类:继承的类,...

  • Day16-面向对象和pygame

    一、类的继承 python中类支持继承,并且支持多继承 1.什么是继承 父类(超类):被继承的类 子类:去继承父类...

  • 2018-10-19继承、重写、内存管理和认识pygame

    一、类的继承 Python中类支持继承,并且支持多继承 1、什么是继承 父类(超类):被继承的类子类:去继承父类的...

  • day16

    类的继承 python中的类支持继承,并且支持多继承() 1.什么是继承 父类(超类):被继承的类子类:去继承的类...

  • day16

    一、类的继承python中类支持继承,并且支持多继承 1.什么是继承父类(超类):被继承的类子类:去继承父类的类继...

网友评论

      本文标题:类 继承

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