在构造函数中使用重写的方法的替代方法,Java [英] Alternatives to using overridden methods in constructors, Java

查看:213
本文介绍了在构造函数中使用重写的方法的替代方法,Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java项目中,我编码我最后使用在构造函数中重写的方法。类似:

In a Java project I am coding I have ended up using methods that are overridden in constructors. Something like:

class SuperClass {
    SuperClass() {
        intialise();
    }

    protected void initialise() {
        //Do some stuff common to all subclasses
        methodA();
        methodB();
    }

    protected abstract void methodA();

    protected abstract void methodB();
}

class SubClass1() {
    SubClass() {
        super();
    }
    protected void methodA() { //Do something }
    protected void methodB() { //Do something }

}

class SubClass2() {
    SubClass() {
        super();
    }
    protected void methodA() { //Do something else }
    protected void methodB() { //Do something else}

}

我现在意识到,虽然在我的case它工作正常,这是有点危险,因为SubClass方法调用对象它当前只被构造为一个SuperClass对象(在将来添加扩展SuperClass的新类时可能会被忽略)。它也不会在c ++中工作,由于对象的创建方式的差异。

I now realise, that although in my case it works fine, it is somewhat dangerous since SubClass methods are called on an object that has currently only been constructed as a SuperClass object (something that may be overlooked when new classes that extend SuperClass are added in the future). It also wouldn't work in c++ due to differences in how objects are created.

我可以想到的唯一的方法是,这是移动初始化方法调用到具体的类构造函数:

The only way I can think to get round this is to move the initialise method call down to the concrete classes constructor:

   class SuperClass {
    SuperClass() {            
    }

    protected void initialise() {
        methodA();
        methodB();
    }

    protected abstract void methodA();

    protected abstract void methodB();
}

class SubClass1() {
    SubClass() {
        super();
        initialise();
    }
    protected void methodA() { //Do something }
    protected void methodB() { //Do something }

}...

这是解决这个问题的常见方法吗?这似乎是一个耻辱(和容易忘记)所有进一步的类,扩展SuperClass需要记住调用initialise()。

Is this the common way to over come this issue? It seems a shame (and easy to forget) that all further classes that extend SuperClass need to remember to call initialise().

我也发现自己做类似的事情更复杂的情境,在构造函数中使用工厂方法,它在子类中被覆盖以决定要实现哪个具体类。我可以想到的唯一的其他方式来绕过这一点,保持设计模式,是可能在两个阶段的过程中构建;

I also found myself doing something similar in a more complicated situational that uses a Factory Method in a constructor, which is overridden in subclasses to decide which concrete class to implement. The only other way I can think to get round this and keep the design pattern as is, is to perhaps construct in a two phase process; i.e. construct with the bare minimum, and then call a second method to finish off the job.

推荐答案

这真的不是一个好的方法想法作为你的子类将不会正确构造当它的methodA()和methodB()被调用。这对于延长班级的人来说会很困惑。推荐你使用抽象 init(),而不是dlev在他/她的评论中建议的。

This is really not a good idea as your Subclass will not be properly constructed when its methodA() and methodB() are called. That would be very confusing for people extending the class. Recommend you use an abstract init() instead, as suggested by dlev in his/her comment.

这篇关于在构造函数中使用重写的方法的替代方法,Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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