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

前端 未结 7 1052
猫巷女王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

    I don't know of any way to do this with a method.

    However, note that this is exactly how constructors work. Every constructor must, directly or indirectly, call one of its superclass's constructors. This is statically guaranteed.

    I note that you are writing an init method. Could you refactor so that your code uses constructors rather than init methods? That would give you this behaviour right out of the gate. Some people (eg me) prefer constructors to init methods anyway, partly for just this reason.

    Note that using constructors rather than init methods might not mean using them on the class you're currently looking at - there might be a refactoring which moves the state needing initialisation out into a parallel class hierarchy which can use proper constructors.

提交回复
热议问题