calling a super method from a static method

后端 未结 6 1448
天涯浪人
天涯浪人 2020-12-05 18:14

Is it possible to call a super static method from child static method?

I mean, in a generic way, so far now I have the following:

public class BaseCo         


        
6条回答
  •  生来不讨喜
    2020-12-05 18:53

    The official name of your implementation is called method hiding. I would suggest introducing a static init(Controller controller) method, and calling an instance method to take advantage of overriding.

    public class Controller {
       static void init(Controller controller) {
          controller.init();
       }
    
       void init() {
          //init stuff
       }
    }
    
    public class BaseController extends Controller {
    
       @override
       void init() {
          super.init();
          //base controller init stuff
       }
    
    }
    
    public class ChildController extends BaseController {
       @override
       void init() {
          super.init();
          //child controller init stuff
       }
    }
    

    You can then call Controller.init(controllerInstance).

提交回复
热议问题