Jump over parent constructor to call grandparent's

后端 未结 10 1840
遥遥无期
遥遥无期 2020-12-18 22:27

The problem is this: I have an abstract class that does some work in its constructor, and a set of child classes that implement the abstract class:

class Abs         


        
10条回答
  •  醉话见心
    2020-12-18 22:54

    Two comments: Firstly, you're not supposed to think in terms of 'jumping over' constructors like this. Secondly, it really sounds like you need to rethink your class relationships.

    Any time you find yourself thinking "A extends B, except that..." is a very good time to look at things further. 'Extends' implies 'is a', which is an either/or relationship: having optional behaviour adds grey areas which will bite you later on.

    As people have said, you could provide multiple constructors on ConcreteClass1 to do the initialization you require in each case, maybe making them protected so that they can only be used by subclasses. But here's a question: what if someone wants to write CustomizedClass2 that needs some (but not all) of the functionality in ConcreteClass1? Do you add another custom constructor?

提交回复
热议问题