简体   繁体   中英

Extending a class with multiple constructors having differing behavior

Given a Class with multiple constructors

class Food {
    int fibre_count;
    int flavour_amount;

    PreservationProcess preserve;

    public Food( int fibre_count, int flavour_amount){
           this.fibre_count = fibre_count;
           this.flavor_amount = flavor_amount;

           preserve = new PreservationProcess("Freshly Picked");

           /*do some other complicated thing that you do _not_ want done in
             the other constructor*/

    }

    public Food (int fibre_count, int flavour_ammount, PreservationProcess preserve){
           this.fibre_count = fibre_count;
           this.flavor_amount = flavor_amount;
           this.preserve = preserve;
           this.flavour_amount *= preserve.getFlavourModifier();
    }
}

and a subclass

class Broccoli extends Food {
    int vitamin_count;

    SomeOtherObj = SO_Obj;
    int branch_count;

    Broccoli(int fibre_count, int flavor_amount, int vitamin_count){

        super(fibre_count, flavour_amount);

        /*common code between the two constructors \/  \/  \/ */
        this.vitamin_count = vitamin_count;

        SO_Obj = new SomeOtherObject();
        branch_count = 4;
        greenness = 13;
        /*common code between the two constructors /\  /\  /\ */
    }

    Broccoli(int fibre_count, int flavor_amount, PreservationProcess preserve, int vitamin_count){

        super(fibre_count, flavour_amount, preserve);

        /*common code between the two constructors \/  \/  \/ */
        this.vitamin_count = vitamin_count;
        SO_Obj = new SomeOtherObject();
        branch_count = 4;
        greenness = 13;
        /*common code between the two constructors /\  /\  /\ */
    }


}

What is the accepted way to include the code shared between the two broccoli constructors? It seems that my options, are either mainatain two separeate copies of the same code in the separate functions, or, create an "init()" or "construct()" function that holds the shared code once, and is called from each constructor. Are there any other options I am missing?

What is generally accepted as the cleanest way to deal with this situation (I'm looking for best practices, not opinions on what people think is best.)

Thanks

You can call this(param1,param2) to invoke one constructor from the other.

Broccoli(int fibre_count, int flavor_amount, int vitamin_count){
    this (fibre_count,flavor_amount,some_default_preserve,vitamin_count);
}

You should always call the constructor with more parameters from the contsructor with less parameters, and give default values to the additional parameters.

最佳实践是使用this(..)调用另一个构造函数,但是如果两个构造函数之间只有几行代码是通用的,那么我认为唯一的选择是实现一个通用方法并从构造函数中调用该方法。

You can have the code in one constructor and call it from another by using

this (parameters list);

Hopefully an if else block is enough to differentiate between different flows.

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