简体   繁体   中英

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. 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. 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. 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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