面向对象 多态

多态:同一接口不同实现,这个在前端用的也比较少
同一个接口,不同表现。js应用极少。需要结合java等语言的接口,重写,重载等功能
class People {
    constructor(name) {
        this.name = name
    }
    saySomething() {

    }
}

class A extends People {
    constructor(name) {
        super(name)
    }
    saySomething() {
        alert('I am A')
    }
}

class B extends People {
    constructor(name) {
        super(name)
    }
    saySomething(){
        alert('I am B')
    }
}

let a = new A('a');
a.saySomething();
let b = new B('b');
b.saySomething();

比如saySomething,定义了一个方法,但是在子类中可以实现不同的功能

多态保持了子类的开放性和灵活性
面向接口编程

猜你喜欢

转载自www.cnblogs.com/wzndkj/p/11706687.html