读书人

怎么判断子类是不是某类的子类

发布时间: 2012-02-20 21:18:24 作者: rapoo

如何判断子类是不是某类的子类?
比如有个父类A,有若干子类B,C等,如何判断类B,类C是从类A继承的?

[解决办法]
你看下面的代码就知道了,判断一个类的父类。
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage(Label1.ClassName);
showmessage(Label1.ClassParent.ClassName);
showmessage(Label1.ClassParent.ClassParent.ClassName);
end;

end.
[解决办法]
InheritsFrom()

如:
if Button1.InheritsFrom(TButton) then
showmessage('TButton是Button1的祖先');

if B.InheritsFrom(A) then
showmessage('A是B的祖先');

读书人网 >.NET

热点推荐