Same instance referred to by multiple constructors
I have an instance of Class 开发者_如何学运维A that I want to refer to in the constructor of multiple instances of B. How can I refer to that particular instance of Class A in each new instance of B?
If you only ever want to have one instance of class A, use a Singleton Pattern. You can then have class B's constructor refer to the singleton. Otherwise, the best way to refer to an object of class A in the constructor of class B is to pass it as an argument.
精彩评论