How do I force a polymorphic call to the super method?

前端 未结 7 1045
猫巷女王i
猫巷女王i 2020-12-16 15:54

I have an init method that is used and overridden through out an extensive heirarchy. Each init call however extends on the work that the previous did. So naturally, I would

7条回答
  •  庸人自扰
    2020-12-16 16:01

    Rather than trying to do that -- I don't think it's achievable btw! -- how about a different approach:

    abstract class Base {
     public final void baseFunction() {
       ...
       overridenFunction(); //call the function in your base class
       ...
     }
    
     public abstract void overridenFunction();
    }
    ...
    class Child extends Base {
     public void overridenFunction() {...};
    }
    
    ...
    Base object = new Child();
    object.baseFunction(); //this now calls your base class function and the overridenFunction in the child class!
    

    Would that work for you?

提交回复
热议问题