composite inheritance: how to assign a final field at sub-class constructor which depends on 'this' value (backward reference)?

后端 未结 2 1605
南笙
南笙 2021-01-25 16:05

I use composite classes to group functionalities.
But, the class A (with composite A1), got inherited by B (with composite B1), and a behavior existent at A1 is going to be

2条回答
  •  旧时难觅i
    2021-01-25 16:07

    B(){ super.a1=new B1(this); } //FAIL: cant change final value

    You can not assign values to already assigned final variables, You can instantiate it within the constructor but not as following.

    B(){super(new B1(this));} //FAIL: cant use 'this' or 'super'

    You can use this once the instance is created only. Since there is a inheritance tree, it is not completely instantiated, So the compiler complains,

    cannot reference this before supertype constructor has been called

提交回复
热议问题