Is it possible to do computation before super() in the constructor?

前端 未结 7 692
深忆病人
深忆病人 2020-12-23 11:17

Given that I have a class Base that has a single argument constructor with a TextBox object as it\'s argument. If I have a class Simple of the following form:



        
7条回答
  •  悲哀的现实
    2020-12-23 11:43

    The reason why the second example is allowed but not the first is most likely to keep the language tidy and not introduce strange rules.

    Allowing any code to run before super has been called would be dangerous since you might mess with things that should have been initialized but still haven't been. Basically, I guess you can do quite a lot of things in the call to super itself (e.g. call a static method for calculating some stuff that needs to go to the constructor), but you'll never be able to use anything from the not-yet-completely-constructed object which is a good thing.

提交回复
热议问题