Delphi:方法“创建"隐藏了基类的虚拟方法 - 但它就在那里 [英] Delphi: Method 'Create' hides virtual method of base - but it's right there

查看:27
本文介绍了Delphi:方法“创建"隐藏了基类的虚拟方法 - 但它就在那里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑假设的对象层次结构,从:

Consider the hypothetical object hierarchy, starting with:

TFruit = class(TObject)
public
    constructor Create(Color: TColor); virtual;
end;

及其后代:

TApple = class(TFruit)
public
    constructor Create(); overload; virtual;
    constructor Create(Color: TColor); overload; override; //deprecated. Calls other constructor - maintaining the virtual constructor chain
end;

这里的想法是我已经覆盖基类的虚拟构造函数,重载也恰好是虚拟的.

The idea here is that I've overridden the virtual constructor of the base class, with an overload that also happens to be virtual.

Delphi 抱怨:

方法Create"隐藏基类型TFruit"的虚方法

Method 'Create' hides virtual method of base type 'TFruit'

除非它没有隐藏它 - 它就在那里!

Except it doesn't hide it - it's right there!

  • 覆盖了祖先中的虚方法,并且
  • 用另一个版本重载了

怎么回事?

推荐答案

两种解决方案:

type
  TFruit = class(TObject)
  public
    constructor Create(Color: TColor); virtual;
  end;

  TApple = class(TFruit)
  public
    constructor Create(); reintroduce; overload;
    constructor Create(Color: TColor); overload; override;
  end;

或者:

type
  TFruit = class(TObject)
  public
    constructor Create; overload; virtual; abstract;
    constructor Create(Color: TColor); overload; virtual;
  end;

  TApple = class(TFruit)
  public
    constructor Create(); override;
    constructor Create(Color: TColor); override; 
  end;

这篇关于Delphi:方法“创建"隐藏了基类的虚拟方法 - 但它就在那里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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