Delphi:如何在虚拟方法上调用继承的继承祖先? [英] Delphi: How to call inherited inherited ancestor on a virtual method?

查看:149
本文介绍了Delphi:如何在虚拟方法上调用继承的继承祖先?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重写了一个虚拟方法,我想调用继承。但是我不想打电话给直接祖先,我想在之前调用一个

i'm overriding a virtual method, and i want to call inherited. But i don't want to call the immediate ancestor, i want to call the one before.

TObject
   TDatabaseObject
      TADODatabaseObject <---call this guy
         TCustomer        <---skip this guy
            TVIP           <---from this guy

我试着投我的自己作为祖先,并调用方法,但它导致递归堆栈溢出:

i tried casting myself as the ancestor, and call the method on that, but it led to recursive stack overflow:

procedure TVip.SetProperties(doc: IXMLDOMDocument);
begin
   TADODatabaseObject(Self).SetProperties(doc); //skip over TCustomer ancestor
   ...
end;

我尝试添加继承的关键字,但这不编译:

i tried adding the inherited keyword, but that doesn't compile:

procedure TVip.SetProperties(doc: IXMLDOMDocument);
begin
   inherited TADODatabaseObject(Self).SetProperties(doc); //skip over TCustomer ancestor
   ...
end;

可能?

推荐答案

你不能用常规的语言方式,因为这会打破面向对象的语言方面。

You can't in a regular language way, as this would break the object oriented aspects of the language.

你可以用指针和聪明的人要这样做,但在甚至开始回答之前:这真的是你想要的吗?

You can fiddle around with pointers and clever casts to do this, but before even starting to answer that: is this really what you want?

正如其他人所说:你的需要听起来像一个严重的设计气味 (类似于代码气味,但更严重。

As others mentioned: your need sounds like a serious "design smell" (which is similar to code smell, but more severe.

编辑:

沿着指针走下路可能会在短期内节省您的工作,并花费几周时间长期的工作。

这使得一些很好的阅读:上游决策,下游成本

Going down the pointer fiddling road might save you work in the short term, and cost you weeks of work in the long term.
This makes for some good reading on that: Upstream decisions, downstream costs.

这篇关于Delphi:如何在虚拟方法上调用继承的继承祖先?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆