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

查看:75
本文介绍了从 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?

是的.不要那样做.(这是所有语言的问题).

构造函数的目的是初始化实例而不是别的.将副作用的调用留给调用者.这将确保所有子构造函数也将完成初始化.

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 = song;
    }
    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天全站免登陆