是什么导致 "W1010 Method '%s' hides virtual method of base type '%s'" 警告?
What causes "W1010 Method '%s' hides virtual method of base type '%s'" warning?
我有一个带虚函数的基础class:
TMyBaseClass = class(TObject)
public
ValueOne : integer;
procedure MyFunction(AValueOne : integer); virtual;
end;
procedure TMyBaseClass.MyFunction(AValueOne : integer);
begin
ValueOne := ValueOne;
end;
后代 class 实现了同名函数。
此函数添加一个新参数并调用其锚函数。
TMyDerivedClass = class(TMyBaseClass)
public
ValueTwo : integer;
procedure MyFunction(AValueOne : integer; AValueTwo : integer);
end;
procedure TMyDerivedClass.MyFunction(AValueOne : integer; AValueTwo : integer);
begin
inherited MyFunction(AValueOne);
ValueTwo := ValueTwo;
end;
编译时,显示以下警告消息:W1010 方法
'MyFunction' hides virtual method of base type 'TMyBaseClass'
我在 another question 中找到了问题的解决方案,但我想知道是什么导致了此警告。
TMyDerivedClass.MyFunction 是否会隐藏 TMyBaseClass.MyFunction 即使两个函数具有不同的参数?如果是,为什么?
documentation 很清楚地解释了这个问题:
You have declared a method which has the same name as a virtual method in the base class. Your new method is not a virtual method; it will hide access to the base's method of the same name.
隐藏 的意思是,从派生的 class 中,您不再有权访问基 class 中声明的虚拟方法。您不能引用它,因为它与派生 class 中声明的方法同名。后一种方法是从派生 class 中可见的方法。
如果两个方法都标有 overload
指令,那么编译器可以使用它们的参数列表来区分它们。如果没有它,编译器所能做的就是隐藏基本方法。
阅读其余链接文档以获取有关潜在解决方案的建议。
我有一个带虚函数的基础class:
TMyBaseClass = class(TObject)
public
ValueOne : integer;
procedure MyFunction(AValueOne : integer); virtual;
end;
procedure TMyBaseClass.MyFunction(AValueOne : integer);
begin
ValueOne := ValueOne;
end;
后代 class 实现了同名函数。 此函数添加一个新参数并调用其锚函数。
TMyDerivedClass = class(TMyBaseClass)
public
ValueTwo : integer;
procedure MyFunction(AValueOne : integer; AValueTwo : integer);
end;
procedure TMyDerivedClass.MyFunction(AValueOne : integer; AValueTwo : integer);
begin
inherited MyFunction(AValueOne);
ValueTwo := ValueTwo;
end;
编译时,显示以下警告消息:W1010 方法
'MyFunction' hides virtual method of base type 'TMyBaseClass'
我在 another question 中找到了问题的解决方案,但我想知道是什么导致了此警告。 TMyDerivedClass.MyFunction 是否会隐藏 TMyBaseClass.MyFunction 即使两个函数具有不同的参数?如果是,为什么?
documentation 很清楚地解释了这个问题:
You have declared a method which has the same name as a virtual method in the base class. Your new method is not a virtual method; it will hide access to the base's method of the same name.
隐藏 的意思是,从派生的 class 中,您不再有权访问基 class 中声明的虚拟方法。您不能引用它,因为它与派生 class 中声明的方法同名。后一种方法是从派生 class 中可见的方法。
如果两个方法都标有 overload
指令,那么编译器可以使用它们的参数列表来区分它们。如果没有它,编译器所能做的就是隐藏基本方法。
阅读其余链接文档以获取有关潜在解决方案的建议。