简体   繁体   English

Delphi:方法“创建”隐藏了基类的虚拟方法 - 但它就在那里

[英]Delphi: Method 'Create' hides virtual method of base - but it's right there

Consider the hypothetical object hierarchy, starting with:考虑假设的对象层次结构,从以下开始:

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

and its descendant:及其后代:

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 complains:德尔福抱怨:

Method 'Create' hides virtual method of base type 'TFruit'方法“Create”隐藏基类型“TFruit”的虚拟方法

Except it doesn't hide it - it's right there!除了它没有隐藏它 - 它就在那里!

  • I overrode the virtual method in the ancestor, and覆盖了祖先中的虚拟方法,并且
  • I overloaded it with another version我用另一个版本重载了

What's the deal?这是怎么回事?

Two solutions:两种解决方案:

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;

Or:要么:

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;

This appears to be a "which came first" sort of issue.这似乎是一个“先来”的问题。 (It appears NGLN found a solution.) (看来 NGLN 找到了解决方案。)

There's another solution, also.还有另一个解决方案。 You can use a default parameter:您可以使用默认参数:

interface

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

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

implementation

{ TFruit }

constructor TFruit.Create(Color: TColor);
begin
  inherited Create;
end;

{ TApple }

constructor TApple.Create(Color: TColor);
begin
  inherited;
end;

// Test code
var
  AppleOne, AppleTwo: TApple;
begin
  AppleOne := TApple.Create;
  AppleTwo := TApple.Create(clGreen);
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “方法'%s'隐藏基类型'%s'的虚拟方法”。 真正被隐藏的是什么? - “Method '%s' hides virtual method of base type '%s'”. What's really being hidden? 是什么导致“W1010方法'%s'隐藏基类型'%s'的虚拟方法”警告? - What causes “W1010 Method '%s' hides virtual method of base type '%s'” warning? SerialForms.pas(17):W1010方法'Create'隐藏基类型'TComponent'的虚方法 - SerialForms.pas(17): W1010 Method 'Create' hides virtual method of base type 'TComponent' delphi重载,覆盖,虚方法 - delphi overload, override, virtual method 隐藏基类的虚方法有什么问题? - What's wrong with hiding virtual method of a base class? 是否可以在Delphi中创建类型方法? - Is it possible to create a type method in Delphi? Delphi:字符串加密方法和base64 - Delphi: string encryption method and base64 Delphi RTTI TVirtualMethodInterceptor.Create不支持具有重载虚拟方法的类 - Delphi RTTI TVirtualMethodInterceptor.Create doesn't support the class that has overload virtual method 如何在Delphi中获取Visual Studio的“创建方法存根”或“声明方法”重构? - How can I get Visual Studio's “Create method stub” or “Declare method” refactoring in Delphi? Delphi:如何在虚方法上调用继承的继承祖先? - Delphi: How to call inherited inherited ancestor on a virtual method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM