Używam Delphi 2007 do zachowania starego projektu, mam problem z dostępem do stałych klas ze zmiennej klasy Reference, zawsze uzyskuję stałą klasę rodziców zamiast dzieci .Dostęp do stałych klas ze zmiennej klasy Reference w Delphi
Załóżmy, że mamy klasę nadrzędną, niektóre klasy potomne, odwołanie do klasy i wreszcie tablicę const do przechowywania referencji klas dla celów zapętlenia.
spojrzeć na następujące prostego programu:
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 := ChildClasses[i];
writeln(c.ClassName, ' -> ', c.ClassConst);
end;
writeln;
writeln('accessing classes directly');
writeln(TChild1.ClassName, ' -> ', TChild1.ClassConst);
writeln(TChild2.ClassName, ' -> ', TChild2.ClassConst);
except
on E: Exception do
Writeln(E.Classname, ': ', E.Message);
end;
end.
Kiedy skończy uzyskać:
looping through class reference array
TChild1 -> BASE CLASS
TChild2 -> BASE CLASS
accessing classes directly
TChild1 -> CHILD 1
TChild2 -> CHILD 2
Spodziewałem się zobaczyć 'dziecko 1' i 'dziecko 2' również w pętli tablicy!
Czy ktoś może mi wyjaśnić, dlaczego nie działa z odniesieniem do klasy?
Trzeba metodę wirtualną wdrożyć polimorfizm . . – kludg