根据this answer(Jim McKeeth,毫无疑问是正确的):
If you declare a method in a descendant class that has the same name as a method in an ancestor class then you are hiding that ancestor method – meaning if you have an instance of that descendant class (that is referenced as that class) then you will not get the behavior of the ancestor. The compiler will give you a warning.
但是,令我惊讶的是这段代码没有给我一个警告:
unit Unit1; interface {$WARNINGS ON} {$WARN HIDING_MEMBER ON} {$WARN HIDDEN_VIRTUAL ON} // I understand the two lines above are superfluous. // I put them there to demonstrate that I have tried to enable these // warnings explicitly. type TBase = class public SomeMember: integer; procedure Foo; end; type TDerived = class (TBase) public SomeMember: integer; procedure Foo; end; implementation { TBase } procedure TBase.Foo; begin end; { TDerived } procedure TDerived.Foo; begin end; end.
我正在使用Delphi XE,我的编译器说一切都很好:
Checking project dependencies…
Building Project1.dproj (Debug,Win32)
dcc command line for “Project1.dpr”
c:\program files\embarcadero\rad studio\8.0\bin\dcc32.exe -$O- -$W+ -$YD –no-config -B -Q -AWinTypes=Windows;WinProcs=Windows;DbiTypes=BDE;
DbiProcs=BDE;DbiErrs=BDE -DDEBUG -E”C:\Compiler Output” -I”c:\program files\embarcadero\rad studio\8.0\lib\Win32\debug”;”c:\program
files\embarcadero\rad studio\8.0\RaveReports\Lib”;”c:\program files\embarcadero\rad studio\8.0\lib\win32\debug”;”c:\program files\embarcadero\rad
studio\8.0\Imports”;”C:\Users\Public\Documents\RAD Studio\8.0\Dcp”;”c:\program files\embarcadero\rad studio\8.0\include”;”C:\Program
Files\Raize\CS4\Lib\RS-XE”;”c:\program files\embarcadero\rad studio\8.0\lib\win32\release”;”c:\program files\embarcadero\rad
studio\8.0\RaveReports\Lib” -LE”C:\Users\Public\Documents\RAD Studio\8.0\Bpl” -LN”c:\program files\embarcadero\rad studio\8.0\bin\Dcp”
-N0″C:\Compiler Output\DCU” -O”c:\program files\embarcadero\rad studio\8.0\Imports”;”C:\Users\Public\Documents\RAD Studio\8.0\Dcp”;”c:\program
files\embarcadero\rad studio\8.0\include”;”C:\Program Files\Raize\CS4\Lib\RS-XE”;”c:\program files\embarcadero\rad studio\8.0\lib\win32\release”;
“c:\program files\embarcadero\rad studio\8.0\RaveReports\Lib” -R”c:\program files\embarcadero\rad studio\8.0\Imports”;”C:\Users\Public\Documents\RAD
Studio\8.0\Dcp”;”c:\program files\embarcadero\rad studio\8.0\include”;”C:\Program Files\Raize\CS4\Lib\RS-XE”;”c:\program files\embarcadero\rad
studio\8.0\lib\win32\release”;”c:\program files\embarcadero\rad studio\8.0\RaveReports\Lib” -U”c:\program files\embarcadero\rad
studio\8.0\lib\Win32\debug”;”c:\program files\embarcadero\rad studio\8.0\RaveReports\Lib”;”c:\program files\embarcadero\rad
studio\8.0\lib\win32\debug”;”c:\program files\embarcadero\rad studio\8.0\Imports”;”C:\Users\Public\Documents\RAD Studio\8.0\Dcp”;”c:\program
files\embarcadero\rad studio\8.0\include”;”C:\Program Files\Raize\CS4\Lib\RS-XE”;”c:\program files\embarcadero\rad studio\8.0\lib\win32\release”;
“c:\program files\embarcadero\rad studio\8.0\RaveReports\Lib” -K00400000 -NB”c:\program files\embarcadero\rad studio\8.0\bin\Dcp”
-NH”C:\Users\Public\Documents\RAD Studio\8.0\hpp” -NO”C:\Compiler Output\DCU” Project1.dpr
Success
Elapsed time: 00:00:00.2
我的猜测是,我误解了Jim McKeeth的上述引用,或者我在编译器中有一些我不知道的设置(顺便提一下,我在另一台计算机上测试了它,结果相同).任何帮助将不胜感激.
解决方法
HIDDEN_VIRTUAL: Turns on or off warnings produced when a descendant declares a method of the same name as a method in an
ancestor,and the ancestor method is virtual,but the descendant’s
method is not an override.
(See 07001.)HIDING_MEMBER: Turns on or off warnings produced when a descendant declares a new property of the same name as a property in an ancestor.
(See 07002.)
这两个警告都不适用于您的代码.在HIDDEN_VIRTUAL的情况下,您没有任何虚拟方法.在HIDING_MEMBER的情况下,您没有任何属性.