美文网首页
类和类的继承

类和类的继承

作者: 掐指化梦 | 来源:发表于2020-05-09 10:20 被阅读0次

1.类的声明

//es6之前
function A(){
    this.name='A'
}

//es6之后
class A{
    constructor(){
        this.name='A'
    }
}

2.类的继承

1)通过改变this指向
function People(name,age){
    this.name=name
    this.age=age
}
People.prototype.toInfo = function(){
    console.log('name:'+this.name)
    console.log('age:'+this.age)
    console.log('sex:'+this.sex)
}
function Man(name,age){
    this.sex='man'
    People.call(this,name,age)
}
function Woman(name,age){
    this.sex='woman'
    People.call(this,name,age)
}
let man = new Man('张三',20)
let woman = new Woman('李四',18)
console.log(man)
console.log(woman)
image.png

我们可以看到name和age属性实现了继承

man.toInfo()
image.png

当我们调用 toInfo方法的时候提示toInfo不是方法,因我们toInfo我们定义在了People.prototype上,通过改变this指向是没有办法继承prototype属性的

2)原型链继承
function People(){
    this.been = ['北京','上海','广州']
}
People.prototype.toInfo = function(){
    console.log('name:'+this.name)
    console.log('age:'+this.age)
    console.log('sex:'+this.sex)
    console.log('been:'+this.been)
}
function Man(name,age){
    this.sex='man'
    this.name = name
    this.age = age
}
Man.prototype = new People()
    
function Woman(name,age){
    this.sex='woman'
    this.name = name
    this.age = age
}
Woman.prototype = new People()

let man = new Man('张三',20)
let woman = new Woman('李四',18)
man.toInfo()
woman.toInfo()
image.png

我们实现了继承并能调用到对应方法,但这个时候新来了一个Man他除了去过 ['北京','上海','广州'] 还去过 深圳

let man2 = new Man('王五',22)
man2.been.push('深圳')

man.toInfo()
man2.toInfo()
image.png

我们发现给王五添加去过深圳,张三也去过了,
原型继承的弊端:修改原型属性,所有继承自这个原型的对应属性也会修改

2)组合继承
function People(name,age){
    this.name = name
    this.age = age
    this.been = ['北京','上海','广州']
}
People.prototype.toInfo = function(){
    console.log('name:'+this.name)
    console.log('age:'+this.age)
    console.log('sex:'+this.sex)
    console.log('been:'+this.been)
}
function Man(name,age){
    this.sex='man'
    People.call(this,name,age)
}
Man.prototype = new People()
    
function Woman(name,age){
    this.sex='woman'
    People.call(this,name,age)
}
Woman.prototype = new People()

let man = new Man('张三',20)
let woman = new Woman('李四',18)
let man2 = new Man('王五',22)
man2.been.push('深圳')

man.toInfo()
woman.toInfo()
man2.toInfo()

组合继承既能有效继承各个属性,又能隔离原型属性,防止修改属性相互影响

组合继承优化

console.log(man.constructor)
image.png

我们可以看到man.constructor指向People而实际应该指向Man

function People(name,age){
    this.name = name
    this.age = age
    this.been = ['北京','上海','广州']
}
People.prototype.toInfo = function(){
    console.log('name:'+this.name)
    console.log('age:'+this.age)
    console.log('sex:'+this.sex)
    console.log('been:'+this.been)
}
function Man(name,age){
    this.sex='man'
    People.call(this,name,age)
}
//继承People.prototype
Man.prototype = Object.create(People.prototype)
//重新指定constructor
Man.prototype.constructor = Man
    

let man = new Man('张三',20)
console.log(man.constructor)
image.png

相关文章

  • 类和类的继承

    1.类的声明 2.类的继承 1)通过改变this指向 我们可以看到name和age属性实现了继承 当我们调用 to...

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

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

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

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

  • Swift教程之继承

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

  • Swift - 继承

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

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

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

  • 2021-01-19java继承

    1,什么是继承 继承是类与类之间的一种关系,继承的类叫子类,被继承的类叫父类;子类继承父类后,将拥有父类的属性和方...

  • day16-类和对象

    一、类的继承 1.基本概念 父类(超类):被继承的类 子类:去继承父类的类 继承定义:让子类直接拥有父类的属性和方...

  • 13_继承

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

  • 5.13 类的继承以及方法的重写(override) [Swif

    1. 一个类可以继承另一个类的方法、属性和其它特性。当一个类继承其它类时,继承的类叫子类,被继承的类叫父类。继承是...

网友评论

      本文标题:类和类的继承

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