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

查看:19
本文介绍了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

我尝试将 myself 转换为祖先,并在其上调用该方法,但它导致递归堆栈溢出:

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;

我尝试添加 inherited 关键字,但无法编译:

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天全站免登陆