简体   繁体   English

从两个抽象类继承,其中一个还需要另一个基类的部分

[英]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 表示为由LeafsNodes特化组成的树结构,然后具有一些特殊行为,如文本内容、图像或在其特定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内容。

UML图

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 - LeafNode都以不同的方式实现了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.

相关问题 两个抽象类,派生自相同的基础 class。 如何访问从一个抽象 class 到另一个的指针 - Two abstract classes, deriving from the same base class. How can I access a pointer from one abstract class to the other 确定某些类在继承抽象基类的情况下何时使用函数 - Determining when to use a function on certain classes if they inherit an abstract base class 继承自两个父类(继承自一个基类)但有些方法不起作用 - Inherit from two parent class (which inherit from one base class) but some methods don't work 如何从C ++基类继承抽象行为 - How to inherit abstract behavior from c++ base class 从抽象基类的多个部分实现继承? - Inherit from multiple partial implementations of an abstract base class? 从类的模板转换继承基类到基类 - Conversion in template from Classes inherit a base class to base 如何继承从一个基 class 继承的不同类的特定变量? - How to inherit specific variables from different classes inherited from one base class? C ++:对于继承多个类的类,如何从一个类继承某些属性,而从另一个类继承其他属性? - C++: For a class that inherited multiple classes, how to inherit certain attributes from one class and others from the other? 在两个类上协调析构函数,其中一个类需要先清理 - Coordinating destructors on two classes, where one class needs to clean up first 从Abstract Base Class访问另一个Base Classes成员 - Access another Base Classes member from Abstract Base Class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM