关于其他班级班级构成的问题
嗨
想象一下,我有一个 StandardCar 类,它保存对类的 Engine 实例的引用。 可以说,我现在正在引擎类的范围内进行编程,并且我想访问父类的一些成员,这意味着使用包含类(标准汽车)的一些成员
我可以在不保留每个发动机与其所用车辆的第二个参考的情况下完成此操作吗?
谢谢。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不直接。唯一的其他选择是,如果
Car
始终发出在Engine
内发生的操作信号,它可以将对其自身的引用作为方法中的参数传递。否则,您将需要对Car
或引擎内的某些共享接口的引用。Not directly. The only other option would be if a
Car
is always signaling the action to occur within theEngine
, it could pass a reference to itself as a parameter in the method. Otherwise, you'll need a reference to the theCar
or some shared interface within the engine.您需要以某种方式让您的
Engine
了解它所包含的StandardCar
,无论是通过对StandardCar
对象本身的引用还是通过将两者关联起来的其他一些数据结构。但更常见的是,您会持有StandardCar
参考。You'd need to somehow make your
Engine
know about theStandardCar
it's contained in, be it through a reference to theStandardCar
object itself or through some other data structure that associates the two. But more commonly you'd hold aStandardCar
reference.