Hi
Imagine I have a class of StandardCar which holds a reference to an Engine instance of a class. Lets say that I'm programming now in the scope of the engine class and I want 开发者_C百科to access some members of the parent class which means to use some members of the containing class( standard Car) Can I do it without holding a second reference from each engine to the vehicle it is used in?Thanks.
Can I do it without holding a second reference from each engine to the vehicle it is used in?
Not directly. The only other option would be if a Car is always signaling the action to occur within the Engine, it could pass a reference to itself as a parameter in the method. Otherwise, you'll need a reference to the the Car or some shared interface within the engine.
You'd need to somehow make your Engine know about the StandardCar it's contained in, be it through a reference to the StandardCar object itself or through some other data structure that associates the two. But more commonly you'd hold a StandardCar reference.
加载中,请稍侯......
精彩评论