Running a method after the constructor of any derived class

前端 未结 6 1080
时光说笑
时光说笑 2020-12-31 00:07

Let\'s say I have a Java class

abstract class Base {
    abstract void init();
    ...
}

and I know every derived class will have to call <

6条回答
  •  爱一瞬间的悲伤
    2020-12-31 00:29

    if Java had it, we wouldn't see all these init() method calls in the wild.

    "surround child constructor with something" - that cannot be done in pure java. Too bad, because there can be very interesting applications, especially with anonymous class + instance initialization block.

    factory and container - they can be helpful when native new doesn't do the job; but that's trivial and boring, and won't work with anonymous classes.

提交回复
热议问题