[英]Inherit from two abstract classes, where one of them also needs parts of the other base class
This is more of a theoretical question without actual code example and I'm not sure if I am just overthinking this.这更多是一个没有实际代码示例的理论问题,我不确定我是否只是想多了。
The whole thing is part of a GUI implemenation I am working on, where the GUI is represented as a tree structure consisting of Leafs
and Nodes
specializations which then have some special behavior like text content, an image or whatever which gets handled in its specific render()
implementation.整个事情是我正在处理的 GUI 实现的一部分,其中 GUI 表示为由Leafs
和Nodes
特化组成的树结构,然后具有一些特殊行为,如文本内容、图像或在其特定render()
处理的任何内容render()
实施。
However, my problem here is, that i would like to have a Hover
functionality on some GUIs but that Hover
functionality needs !some!但是,我的问题是,我希望在某些 GUI 上具有Hover
功能,但Hover
功能需要一些! of the basic GUI element functions, shown in the diagram, and now I can't wrap my head around how i connect these two parts into the final Button
class.基本 GUI 元素功能的一部分,如图所示,现在我无法理解如何将这两个部分连接到最终的Button
类。 It has to inherit all the Leaf
functionality plus the abstract Hover
stuff.它必须继承所有Leaf
功能以及抽象的Hover
内容。
Is there a way to make this work the way I am imagining it?有没有办法按照我想象的方式完成这项工作? Or do i have to give this Hover
functionality to all GUIs and just implement a blank onMouseEnter()
and onMouseExit()
for the ones that don't use it?或者我是否必须为所有 GUI 提供此Hover
功能,并为不使用它的人实现一个空白的onMouseEnter()
和onMouseExit()
?
The reason I would like to not give it to all GUIs is because the check if the mouse is hovering over the element is not for free.我不想将它提供给所有 GUI 的原因是因为检查鼠标是否悬停在元素上不是免费的。 And not all elements have to be interactive.并非所有元素都必须是交互式的。
A few notes:一些注意事项:
- Hover
might also be inherited by a Node specialization - Hover
也可能被节点特化继承
- The list of functions in Hover
is complete - Hover
中的功能列表已完成
- Leaf
and Node
both implement some of the abstract functions from Element
in different ways - Leaf
和Node
都以不同的方式实现了Element
一些抽象函数
If you can come up with a better title for this, feel free to suggest an edit如果您可以为此想出更好的标题,请随时提出修改建议
I would extract the common functionalities of Element and Hover into a common base class:我会将 Element 和 Hover 的通用功能提取到一个通用基类中:
MouseBase
|
------------------------------
| |
| Element
| |
Hover |
| -------------------------------
| | |
| Leaf Node
---------------
|
Button
In that case, if MouseBase is a virtual base class, Button will get one single version of it, else it will have one via Element inheritance and a second one via Hover inheritance.在这种情况下,如果 MouseBase 是一个虚拟基类,Button 将获得它的一个单一版本,否则它将通过 Element 继承获得一个版本,并通过 Hover 继承获得第二个版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.