Delphi:什么时候重新引入隐藏祖先,什么时候显示它们? [英] Delphi: When does reintroduce hide ancestors and when does it show them?

查看:194
本文介绍了Delphi:什么时候重新引入隐藏祖先,什么时候显示它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天最近在Stackoverflow我学到了:





,所以这里是另一个非常具体的问题,支持我的主要问题处理构造函数






更新:取代了整个问题:

  TComputer = class(TObject)
public
constructor Create(Teapot:string ='');
end;

TCellPhone = class(TComputer)
public
构造函数Create(Cup:Integer);超载;虚拟;
constructor Create(Cup:Integer; Teapot:string);超载;虚拟;
end;

构建TCellPhone时,有3个构造函数可用:




  • 杯:整数

  • 杯:整数; Teapot:string

  • [Teapot:String ='']



code>构造函数(Teapot:string ='')未隐藏?






现在我添加了第三个后代:

  TComputer = class(TObject)
public
构造函数Create Teapot:string ='');
end;

TCellPhone = class(TComputer)
public
构造函数Create(Cup:Integer);超载;虚拟;
constructor Create(Cup:Integer; Teapot:string);超载;虚拟;
end;

TiPhone = class(TCellPhone)
public
构造函数Create(Cup:Integer);覆盖;
end;

当构建 TiPhone 四个构造函数:




  • Cup:Integer

  • Cup:Integer

  • Cup:Integer Teapot:string

  • [Teapot:string ='']



> 四个构造函数?我突破了现有的三个。 修改:这可能是代码洞察中的错误,它显示了我四个 - 但是当两个是相同的时候,我怎么可能调用。



< hr>

再次使用原始代码:

  TComputer = class(TObject)
public
constructor Create(Teapot:string ='');
end;

TCellPhone = class(TComputer)
public
构造函数Create(Cup:Integer);超载;虚拟;
constructor Create(Cup:Integer; Teapot:string);超载;虚拟;
end;

已经知道 TCellPhone 有三个构造函数:




  • 杯:整数

  • 杯:整数; Teapot:string

  • [Teapot:String ='']



声明 TCellPhone 隐藏祖先构造函数?例如所以:

  TNokia = class(TCellPhone)
end;

只有两个构造函数:




  • 杯:整数

  • 杯:整数; Teapot:string




$ b / code>用于隐藏非虚拟祖先。在前一种情况下, TiPhone 有四个构造函数(理想情况下只有两个 - TComputer 以某种方式隐藏其祖先)。但是,即使我不能修复 TComputer ,我可以更改 TiPhone 只有一个:

  TComputer = class(TObject)
public
constructor Create(Teapot:string ='');
end;

TCellPhone = class(TComputer)
public
构造函数Create(Cup:Integer);超载;虚拟;
constructor Create(Cup:Integer; Teapot:string);超载;虚拟;
end;

TiPhone = class(TCellPhone)
public
构造函数Create(Cup:Integer);再引入
end;

现在 TiPhone 只有一个构造函数:




  • 杯:整数



重新导入仅用于抑制关于隐藏虚拟祖先的警告。在这种情况下:

 创建(Teapot:string ='')
pre>

不是虚拟的 - 但我仍然可以使用reintroduce隐藏它。






现在,如果我添加另一个重载到 TiPhone

  TiPhone = class(TCellPhone)
public
constructor Create(Cup:Integer);再引入超载;
constructor Create(Handle:String);超载;
end;

然后突然之前隐藏的祖先回来:




  • TiPhone.Create(7);

  • TiPhone.Create('pink');

  • TiPhone.Create(7,'pink');

  • TiPhone.Create();







  • <
  • 如何显示某些内容


解决方案

您不使用重新引入隐藏祖先类的方法。你只需要声明一个与祖先类中同名的方法,而不重写或重载它。当祖先类的方法(被隐藏的方法)为虚拟时,使用重新引入抑制Delphi引发的警告



如果子孙的方法覆盖祖先的,那么它不会隐藏。



如果后代的方法重载祖先的方法,那么它也不会隐藏。两者都可以调用。


Today Recently on Stackoverflow i learned that:

i've been trying to make sense of it all, so here is a another, very specific question, supporting my main question dealing with constructors.


Update: replaced the entire question:

TComputer = class(TObject)
public
   constructor Create(Teapot: string='');
end;

TCellPhone = class(TComputer)
public
   constructor Create(Cup: Integer); overload; virtual;
   constructor Create(Cup: Integer; Teapot: string); overload; virtual;
end;

When constructing TCellPhone, 3 constructors are avaible:

  • Cup: Integer
  • Cup: Integer; Teapot: string
  • [Teapot: String = '']

Question: Why is constructor(Teapot: string='') not being hidden?


Now i added a 3rd descendant:

TComputer = class(TObject)
public
   constructor Create(Teapot: string='');
end;

TCellPhone = class(TComputer)
public
   constructor Create(Cup: Integer); overload; virtual;
   constructor Create(Cup: Integer; Teapot: string); overload; virtual;
end;

TiPhone = class(TCellPhone)
public
   constructor Create(Cup: Integer); override;
end;

When constructing TiPhone four constructors are available:

  • Cup: Integer
  • Cup: Integer
  • Cup: Integer; Teapot: string
  • [Teapot: string = '']

Why are there four constructors? i overrode one of the existing three. Edit: This may be a bug in code-insight, it shows me four - yet how could i possibly call then when two are the same.


Using the original code again:

TComputer = class(TObject)
public
   constructor Create(Teapot: string='');
end;

TCellPhone = class(TComputer)
public
   constructor Create(Cup: Integer); overload; virtual;
   constructor Create(Cup: Integer; Teapot: string); overload; virtual;
end;

it's already known that TCellPhone has three constructors:

  • Cup: Integer
  • Cup: Integer; Teapot: string
  • [Teapot: String = '']

How do i alter the declaration of TCellPhone to hide the ancestor constructor? e.g. so that:

TNokia = class(TCellPhone)
end;

will only have two constructors:

  • Cup: Integer
  • Cup: Integer; Teapot: string

Now for the case where reintroduce is used to hide a non-virtual ancestor. In the previous case TiPhone has four constructors (ideally there would be only two - with TComputer somehow hiding its ancestor). But even if i can't fix TComputer, i can change TiPhone to only have the one:

TComputer = class(TObject)
public
    constructor Create(Teapot: string='');
end;

TCellPhone = class(TComputer)
public
    constructor Create(Cup: Integer); overload; virtual;
    constructor Create(Cup: Integer; Teapot: string); overload; virtual;
end;

TiPhone = class(TCellPhone)
public
    constructor Create(Cup: Integer); reintroduce;
end;

Now TiPhone has only one constructor:

  • Cup: Integer

Reintroduce is normally only used to suppress the warning about hiding virtual ancestors. In this case:

Create(Teapot: string = '')

isn't virtual - yet i can still use reintroduce to hide it.


But now, if i add another overloaded to TiPhone:

TiPhone = class(TCellPhone)
public
   constructor Create(Cup: Integer); reintroduce; overload;
   constructor Create(Handle: String); overload;
end;

Then suddenly the (previously hidden) ancestors come back:

  • TiPhone.Create(7);
  • TiPhone.Create('pink');
  • TiPhone.Create(7, 'pink');
  • TiPhone.Create();

As you can see, i'm struggling to understand the logic of

  • when something is hidden
  • how to hide something
  • when something is shown
  • how to show something

解决方案

You don't use reintroduce to hide a method of an ancestor class. You do that simply by declaring a method with the same name as one in the ancestor class without overriding or overloading it. You use reintroduce to suppress the warning that Delphi raises when the ancestor class's method (the one being hidden) is virtual.

If the descendant's method overrides the ancestor's, then it's not hiding. Calls to the ancestor's method are routed to the descendant's instead.

If the descendant's method overloads the ancestor's, then it's also not hiding. Both are available to call.

这篇关于Delphi:什么时候重新引入隐藏祖先,什么时候显示它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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