delphi中如何实现多重继承?
我正在对一个旧库进行完全重写,我不确定如何处理这种情况(为了便于理解,大家都欢呼自行车类比):
我有以下类:
TBike - 自行车本身
TBikeWheel
- 自行车车轮之一TBikeWheelFront
和TBikeWheelBack
,两者都继承自TBikeWheel
和然后在其之上实现他们需要的特定内容
这非常简单,但现在我决定创建多种自行车,每辆自行车都有自己的车轮 - 它们与常规前轮/后轮执行相同的操作,加上那辆自行车的具体情况。
TBikeXYZ
- 继承自TBike
TBikeWheelXYZ
- 继承于TBikeWheel
这是我的问题:TBikeWheelFrontXYZ
code> 应该继承自 TBikeWheelXYZ
(获取 XYZ 轮的具体方法),但它也应该继承自 TBikeWheelFront
(获取前轮的具体方法) 。
我的问题是,我怎样才能以一种不这样的方式实现它:
- 感觉就像黑客
- 迫使我多次重写相同的代码
I'm doing a full rewrite of an old library, and I'm not sure how to handle this situation (for the sake of being understood, all hail the bike analogy):
I have the following classes:
TBike
- the bike itselfTBikeWheel
- one of the bike's wheelTBikeWheelFront
andTBikeWheelBack
, both inherits fromTBikeWheel
and then implements the specific stuff they need on top of it
This is pretty straightforward, but now I decide to create multiple kind of bikes, each bikes having it's own kinds of wheel - they do the same stuff as a regular front/back wheels, plus the specific for that bike.
TBikeXYZ
- inherits fromTBike
TBikeWheelXYZ
- inherits fromTBikeWheel
And here is my problem: TBikeWheelFrontXYZ
should inherit from TBikeWheelXYZ
(to get the specific methods of an XYZ wheel), but it should also inherit from TBikeWheelFront
(to get the specific methods of a front wheel).
My question here is, how can I implement that in a way that doesn't:
- feel like a hack
- force me to rewrite the same code several time
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我想建议以下步骤:
从
TBikeWheelXYZ
或TBikeWheelFront
继承TBikeWheelFrontXYZ
类(因为在 Delphi 中多重继承是不可能的)正如上面的答案中提到的)。将父类
TBikeWheelXYZ
或TBikeWheelFront
之一转换为TBikeWheel
类的类帮助器。将类帮助器单元添加到声明
TBikeWheelFrontXYZ
类的单元中。I would like to suggest the following steps:
Inherit the
TBikeWheelFrontXYZ
class from eitherTBikeWheelXYZ
orTBikeWheelFront
(since in Delphi multiple inheritance is impossible as mentioned in the answers above).Convert one of the parent classes
TBikeWheelXYZ
orTBikeWheelFront
to class helper for theTBikeWheel
class.Add the class helper unit to the unit, where the
TBikeWheelFrontXYZ
class is declared.Delphi 不支持多重继承。 但是类可以支持/实现多个接口,并且您可以委托接口实现,因此您可以模拟多重继承。
Delphi does not support Multiple Inheritance. But classes can support / implement multiple interfaces and you can delegate interface implementation, so you can kinda simulate multiple inheritence.
使用接口。 像这样的东西(根据你的描述,在我的脑海中......)
然后为接口实现类,这些类执行旧(大概是 C/C++)库中相应类的操作,并在相应的库中实例化它们类的构造函数。
Use interfaces. Something like this (Off the top of my head, based on your description.....)
Then implement classes for the interfaces that do what the corresponding classes in your old (presumably C/C++) library does and instantiate them in the corresponding class's constructor.
使用多态性将每个“事物”实现为一个对象层次结构,然后依次向该对象添加对象属性。 因此,创建车轮层次结构和自行车层次结构。 然后将车轮添加到自行车作为祖先自行车对象中的字段。 见下文。
Use polymorhism to implment each 'thing' as an object hierarchy in its own right and then add object properties to that object in turn. So, create a hierarchy of wheels, and a hierarchy of bikes. Then add wheels to bikes as fields in the ancestor bike object. See below.
Brian Frost 建议的变体:
A variation of Brian Frost's suggestion:
基本上 - 你不能。 Delphi 不支持多重继承。
因此,留下这样的困境,问题是:你是否可以重构该库,以便你可以摆脱使用接口的束缚? 多重继承主要是关于函数和方法吗? 如果是这样 - 使用接口。 Delphi 可以支持一个类上的多个接口。
如果多重继承更多的是继承类中的实际功能,那么恐怕您可能会考虑更大规模的重构。 您需要找到一种方法来分解这些功能依赖关系,这样您就可以使其继承自单个基类,可能还需要添加一些附加接口。
抱歉,我无法提供一个简单的答案 - 这只是它的现实。
马克
Basically - you CAN'T. Delphi does not support multiple inheritance.
So left with that dilemma, the question is: could you possibly refactor that library in such a way that you can get away with using interface? Is the multiple inheritance mostly about functions and methods? If so - use interfaces. Delphi can support multiple interfaces on a class.
If the multi-inheritance is more about inheriting actual functionality in the classes, then you're probably looking at a bigger scale refactoring, I'm afraid. You'll need to find a way to break up those functional dependencies in such a way you can make it inherit from a single base class, possibly with some additional interfaces thrown in.
Sorry I can't provide an easy answer - that's just the reality of it.
Marc
您可以尝试从 TBikeWheelFront 中提取一个接口,例如 IFrontWheel,这样它就是 TBikeWheel 的子类,但实现了 IFrontWheel。 然后TBikeWheelXYZ继承自TBikeWheel,TBikeWheelFrontXYZ继承自TBikeWheelXYZ并实现IFrontWheel。
然后,您可以定义一个类 TFrontwheel 并为其提供与接口相同的方法,但现在您可以实现它们。 然后 TBikeWheelFront 和 TBikeWheelXYZ 获得类型 TFrontwheel 的私有成员,并且它们的 IFrontWheel 实现简单地委托给私有成员方法。
这样你就不会有双重实现。
You can try to extract an interface, say IFrontWheel, out of TBikeWheelFront, so that it is a subclass of TBikeWheel but implements IFrontWheel. Then TBikeWheelXYZ inherits from TBikeWheel and TBikeWheelFrontXYZ inherits from TBikeWheelXYZ and implements IFrontWheel.
Then you can define a class TFrontwheel and give it the same methods as the interface, but now you implement them. Then TBikeWheelFront and TBikeWheelXYZ get a private member of type TFrontwheel and the IFrontWheel implementations of them simply delegate to the private member methods.
This way you don't have double implementations.
新版本 Delphi 的另一种选择是在组合模型中利用泛型。 这在多个基类(本例中的
TBarA
和TBarB
)不可访问修改(即:框架或库类)的情况下特别有用。 例如(请注意,为简洁起见,此处省略了TFoo
中必要的析构函数
):Another alternative with newer versions of Delphi is to leverage generics in a compositional model. This is particularly useful in the case where the multiple base classes (
TBarA
andTBarB
in this example) are not accessible for modification (ie: framework or library classes). For example (note, the necessarydestructor
inTFoo<T>
is omitted here for brevity) :如果您不想多次重复代码并且想要解耦代码,您可以尝试这种方式。
you can try this way, if you do not want to repeat the code several times and want a decoupled code.
抱歉,Delphi 不支持多重继承。
Sorry, Delphi does not support Multiple Inheritance.