我有很多我的类的子类:
PMyAncestor =^TMyAncestor;
TMyAncestor = object
public
constructor init;
destructor done; virtual;
// There are virtual methods as well
end;
PMyDescendant1 =^TMyDescendant1;
TMyDescendant1 = object ( TMyAncestor )
end;
PMyDescendant2 =^TMyDescendant2;
TMyDescendant2 = object ( TMyAncestor )
end;
PMyDescendant3 =^TMyDescendant3;
TMyDescendant3 = object ( TMyDescendant2 )
end;
procedure foo;
var
pMA1, pMA2, pMA3, pMA4 : PMyAncestor;
s : string;
begin
pMA1 := new( PMyAncestor, init );
pMA2 := new( PMyDescendant1, init );
pMA3 := new( PMyDescendant2, init );
pMA4 := new( PMyDescendant3, init );
try
s := some_magic( pMA1 ); // s := "TMyAncestor"
s := some_magic( pMA2 ); // s := "TMyDescendant1"
s := some_magic( pMA3 ); // s := "TMyDescendant2"
s := some_magic( pMA4 ); // s := "TMyDescendant3"
finally
dispose( pMA4, done );
dispose( pMA3, done );
dispose( pMA2, done );
dispose( pMA1, done );
end;
end;
有没有办法获取其后代实例的类名?我不想为此创建虚拟方法(因为有数千个后代)。我知道有
typeOf(T)
运算符。但它的返回类型是什么?好的,指针。但我应该将它转换成什么?转换为 PTypeInfo
似乎是错误的。
typeOf
应该返回什么类型? - User007TypeOf
的方式就是跟一个指定类型进行比较:if TypeOf(pMA1^) = TypeOf(TMyAncestor) then ...
。 - LU RD