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

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

    Make the class at the top of the inheritance tree set a flag on initialization. Then a class in the bottom of the inheritance tree can check for that flag to see if the whole tree has been traversed. I would make documentation that every child of base should include the following init code:

    super.init()
    if (!_baseIsInitialized) {
        // throw exception or do w/e you wish
    }
    

    where base uses

    _baseIsInitialized = true;
    

    The other way around, forcing your childs to call super.init() is a lot thougher and would most likely include ugly hacks.

    0 讨论(0)
提交回复
热议问题