我正在使用Delphi 2007来维护一个旧项目,我在从Class Reference变量访问类常量时遇到问题,我总是得到父类常量而不是子类常量.
假设有一个父类,一些子类,一个类引用,最后是一个const数组来存储类引用以进行循环.
看看以下简单的程序:
program TestClassConst;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
TParent = class
const
ClassConst = 'BASE CLASS';
end;
TChild1 = class(TParent)
const
ClassConst = 'CHILD 1';
end;
TChild2 = class(TParent)
const
ClassConst = 'CHILD 2';
end;
TParentClass = class of TParent;
TChildClasses = array[0..1] of TParentClass;
const
ChildClasses: TChildClasses = (TChild1, TChild2);
var
i: integer;
c: TParentClass;
s: string;
begin
try
writeln;
writeln('looping through class reference array');
for i := low(ChildClasses) to high(ChildClasses) do begin
c := …Run Code Online (Sandbox Code Playgroud)