Is it possible for two classes to be data members of each other mutually?

后端 未结 2 1800
情话喂你
情话喂你 2021-01-23 15:57

Is it possible for a class A to be a field of another class B and at the same time, for class B to be a field of Class A?

I have a scenario where I have two classes: Mat

2条回答
  •  孤独总比滥情好
    2021-01-23 16:32

    Yes it is possible, but I doubt it can be achieved during object construction time, which causes cyclic dependency.

    First create classA and classB objects and assign the mutual reference by using setter of each objects.

提交回复
热议问题