//
procedure TForm1.Button3Click(Sender: TObject);
var
MyClass: TClass;
begin
{對象的 ClassParent 方法返回的也是 TClass, 可以把對象的 ClassParent 賦予 TClass 變量}
MyClass := Self.ClassType;
ShowMessage(MyClass.ClassName); {TForm1 - Form1 的類名}
MyClass := MyClass.ClassParent;
ShowMessage(MyClass.ClassName); {TForm - Form1 的父類名}
MyClass := MyClass.ClassParent;
ShowMessage(MyClass.ClassName); {TCustomForm - Form1 的爺爺類名}
end;
//遍歷窗體的所有父類
procedure TForm1.Button4Click(Sender: TObject);
var
MyClass: TClass;
begin
MyClass := Self.ClassType;
while MyClass <> nil do
begin
ShowMessage(MyClass.ClassName);
MyClass := MyClass.ClassParent;
end;
{將依次顯示:
TForm1
TForm
TCustomForm
TScrollingWinControl
TWinControl
TControl
TComponent
TPersistent
TObject
}
end;
end.