Java继承两个类
我正在使用 java 中的一个接口,它与 PureData 进行通信。为此,我的类必须扩展给定的类 MaxObject
。在设计我的类(一个循环缓冲区)时,我发现我需要扩展 java 的 Iterator
类。所以我必须同时延长两节课。
我的猜测是,唯一的解决方案是创建两个不同的类,并让其中一个类成为另一个类的组件。但是,这是唯一的解决方案吗?这是最好的吗?
此外,每当我发现自己需要从两个类继承时,是否是因为设计不好?有没有设计模式来解决这个类?
谢谢
I'm using an interface in java, that communicates with PureData. In order to do so, my classes have to extend a given class MaxObject
. While designing my class, which is a cirular buffer, I discovered that I need to extend java's Iterator
class. So I have to extend two classes at the same time.
My guess is that the only solution is to create two different classes and let one of them be a component of the other one. But, is it the only solution? Is it the best one?
Further, whenever I find myself needing inherit from two classes, is it a because of a bad design? Is there a design pattern to solve this class?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Iterator
不是一个类,它是一个接口。因此,您不需要扩展它,而是实现它。您可以实现任意数量的接口 - 唯一的限制是您只能扩展一个类。在你的情况下:
Iterator
is not a class, it's an interface. As such, you don't extend it, you implement it. You can implement any number of interfaces - the only limitation is that you can only extend one class.In your case:
编辑:我应该仔细阅读正在扩展的内容。 EboMike 是对的,您不需要扩展 Iterator 类。
听起来像 DDofD: http://javacodeonline.blogspot.com/ 2009/08/deadly-diamond-of-death.html
edit: I should have read closer what's being extended. EboMike is right, you don't need to extend the Iterator class.
Sounds like the DDofD: http://javacodeonline.blogspot.com/2009/08/deadly-diamond-of-death.html
Iterator
是一个接口。从理论角度来看,没有什么反对扩展 MaxObject 和实现 Iterator 的。由于缺乏信息,我不能说这样做是否是个好主意,但我有一种不好的预感。
Iterator
is an interface. From a theoretical point of view there's nothing against extending MaxObject and implementingIterator
.Due to a lack of information I cannot say if it's a good idea to do this, but I have a bad feeling.