我有一个带有虚函数的基类:
TMyBaseClass = class(TObject)
public
ValueOne : integer;
procedure MyFunction(AValueOne : integer); virtual;
end;
procedure TMyBaseClass.MyFunction(AValueOne : integer);
begin
ValueOne := ValueOne;
end;
后代类实现具有相同名称的函数。
该函数添加一个新参数并调用其锚定函数。
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 Method
“MyFunction”隐藏基类型“TMyBaseClass”的虚拟方法
我找到了阅读问题的解决方案另一个问题 https://stackoverflow.com/questions/9103263/delphi-method-create-hides-virtual-method-of-base-but-its-right-there,但我想知道是什么导致了这个警告。
即使两个函数具有不同的参数,TMyDerivedClass.MyFunction 是否也会隐藏 TMyBaseClass.MyFunction?如果是这样,为什么?
The 文档 http://docwiki.embarcadero.com/RADStudio/en/W1010_Method_'%25s'_hides_virtual_method_of_base_type_'%25s'_(Delphi)很清楚地解释了这个问题:
您已声明一个与基类中的虚拟方法同名的方法。你的新方法不是虚拟方法;它将隐藏对基类同名方法的访问。
是什么意思hiding是从派生类中您不再有权访问基类中声明的虚拟方法。您不能引用它,因为它与派生类中声明的方法具有相同的名称。后一个方法是从派生类中可见的方法。
如果两种方法都标有overload
指令,那么编译器可以使用它们的参数列表来区分它们。如果没有这个,编译器所能做的就是隐藏基本方法。
请阅读链接文档的其余部分,以获取有关潜在解决方案的建议。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)