简体   繁体   English

在Java中的父构造函数中隔行调用子构造函数

[英]Calling child constructor interlaced in parent constructor in java

I have lots of children to a base class and plan for adding a lot more. 我有很多孩子上基础课,并计划增加更多孩子。 I'm lazy. 我很懒。 The child creator sets up some basic things that is needed for the super constructor and vice versa. 子创建者设置了超级构造函数所需的一些基本内容,反之亦然。 A simple solution from my problem would be the following: 我的问题的一个简单解决方案是:

parent {
   public parent(){/*some code*/}
   public void finalSetup(){/*code that dependent on the fact that the child constructor has run*/}
}
child{
    public child(){/*some code;*/ super.finalSetup();}
}

How ever, calling super.finalSetup() on every child is quite the hassle, and if I forget it on one it'll break. 但是,在每个孩子上调用super.finalSetup()确实很麻烦,而且如果我一次忘记它,它就会崩溃。 That's no good. 不好 My question is simple: is there any way to set this up form the parent. 我的问题很简单:是否有任何方法可以将其设置为父级。 As far as my google skills go I haven't been able to find one. 就我的Google技能而言,我一直找不到。 Hopefully you guys know something I don't. 希望你们知道我不知道的东西。

Thanks 谢谢

This should do it. 这应该做。 The basic idea is to override the before and after methods in your children and in the parent constructor you simply run both and do some initialization in between. 基本思想是在子级和父级构造函数中覆盖before和after方法,您只需运行二者并在它们之间进行一些初始化。 Of course this does not save you from forgetting to call the parent constructor. 当然,这不能使您避免忘记调用父构造函数。

abstract class Parent {

    Parent(){
        doBefore();
        // some stuff
        doAfter();
    }

    abstract void doBefore();

    abstract void doAfter();


}

class Child extends Parent {

    Child(){
        super();
    }

    void doBefore(){
        // do before stuff
    }

    void doAfter(){
        // do after stuff
    }
}

With abstract methods in your parent you can implement any permutation of before / after procedures. 在您的父级中使用abstract方法时,您可以实现before / after过程的任何排列。

This should be what you want, but as already mentioned, it can be not the best idea . 这应该是您想要的,但是正如已经提到的,它可能不是最好的主意 You don't need to explicitly call the parent constructor in your subclass if you have a no-argument constructor in your superclass. 如果超类中没有自变量构造函数,则无需在子类中显式调用父构造函数。

abstract class Parent {
    Parent() {
        /*some code*/
        childInit();
        finalSetup();
    }
    void finalSetup() {/*code that dependent on the fact that the child constructor has run*/}
    abstract void childInit();
}

class Child extends Parent {
    @Override
    void childInit() {
        /* the code you would put in child's constructor */
    }
}

Consider the Factory pattern to create generic type that extends Parent. 考虑使用Factory模式来创建扩展Parent的泛型类型。

public class Parent {
   public Parent(){/*some code*/}
   public void finalSetup(){/*code that dependent on the fact that the child constructor has run*/}

   public static <T extends Parent> T makeChild(Class <T> klass) {
        T child = null;
        try {
          child = klass.newInstance();
          child.finalSetup();
        } 
        catch (InstantiationException| IllegalAccessException ex) {
           // somthing went wrong
        }
        return child;
    }
}

and call 并打电话

Child child = Parent.makeChild(Child.class);

It is useful when: 在以下情况下很有用:
+ a class can't anticipate the class of objects it must create +类无法预期必须创建的对象的类
+ a class wants its subclasses to specify the fields or objects it creates +一个类希望其子类指定其创建的字段或对象
+ classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate +类将责任委派给几个帮助程序子类之一,并且您想定位哪个帮助程序子类是委托的知识

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM