从ES6中的父类调用子方法 [英] Call a child method from a parent class in ES6

查看:81
本文介绍了从ES6中的父类调用子方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从父类调用子方法是好/坏的做法?

Is It good/bad practice to call a child method from a parent class?

class Parent {
    constructor() {
        // if 'autoPlay' exists (was implemented) in chain
        if (this.autoPlay) {
            this.autoPlay(); // execute from parent
        }
    }
}

class ChildA extends Parent {
    autoPlay() {
        console.log('Child');
    }
}

class ChildB extends Parent {
    // 'autoPlay' wasn't implemented
}

const childA = new ChildA();
const childB = new ChildB();


推荐答案


这是好事练习从父类调用子方法?

Is it a good practice to call a child method from a parent class?

是的,这是一种完全正常的做法。父类只调用实例的某个方法,如果子类重写了方法,则调用子方法。但是,你通常不会做这样的我的实例定义了这个方法测试,你只需要调用它。如果你想在默认情况下什么都不做,只需定义一个空方法(比如@ scipper的答案)。如果你想使方法成为抽象(强制子类来覆盖它),你可以保留它未定义或定义抛出适当异常的方法。

Yes, it's a totally normal practise. The parent class just calls some method of the instance, and if the child class has overridden the method then the child method is called. However, you usually wouldn't do such a "has my instance defined this method" test, you just would call it. If you want to do nothing by default, just define an empty method (like in @scipper's answer). If you want to make the method abstract (force child classes to override it), you can either leave it undefined or define a method that throws an appropriate exception.


从父构造函数调用子方法是不好的做法?

Is is a bad practice to call a child method from a parent constructor?

是的。不要这样做。(这是所有语言中的问题)。

Yes. Don't do that. (It's a problem in all languages).

构造函数的目的是初始化实例,没有别的。将副作用的调用留给调用者。这将确保所有儿童建设者也将完成他们的初始化。

The purpose of a constructor is to initialise the instance and nothing else. Leave the invocations of side effects to the caller. This will ensure that all child constructors will finish their initialisation as well.

一个人为的例子:

class Parent {
    autoPlay() {
        this.play("automatically "); // call child method
    }
    play(x) {
        console.log(x+"playing default from "+this.constructor.name);
    }
}

class ChildA extends Parent {
    // does not override play
}
class ChildB extends Parent {
    constructor(song) {
        super();
        this.song = this;
    }
    play(x) {
        console.log(x+"playing "+this.song+" from ChildB");
    }
}

const child1 = new ChildA();
child1.autoPlay();
const child2 = new ChildB("'Yeah'");
child2.autoPlay();

请注意,如果 Parent 构造函数确实调用 autoplay 。如果您不希望在实例化后需要额外的方法调用,请使用辅助函数。它甚至可能是一个静态方法:

Notice how that would not work if the Parent constructor did call autoplay. If you don't like to need an extra method call everywhere after the instantiation, use a helper function. It might even be a static method:

class Parent {
    autoPlay() { … }
    play { … }
    static createAndAutoPlay(...args) {
        const instance = new this(...args);
        instance.autoPlay();
        return instance;
    }
}
…
const child1 = ChildA.createAndAutoPlay();
const child2 = ChildB.createAndAutoPlay("'Yeah'");

这篇关于从ES6中的父类调用子方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆