---------开发环境Delphi7 ----------
-------------
效果图:
----Unit开始
1 unit Unit1; 2 3 interface 4 5 uses 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, StdCtrls; 8 9 type 10 TSysButton=class of Tbutton; 11 TMyCustomerButton=class(TButton) //声明一个TMyCustomerButton类 Tbutton第一代继承’人‘ 12 private 13 FStr:string; 14 end; 15 TMyButton= class of TMyCustomerButton; //声明一个类引用类型(类的类),具体啥的请搜一下 16 17 TMyCustomerButton02=class(TMyCustomerButton) //Tbutton第二代继承’人‘ 18 private 19 FStr02:string; 20 end; 21 22 TMyCustomerButton03=class(TMyCustomerButton02) //Tbutton第三代继承’人‘ 23 private 24 FStr03:string; 25 end; 26 27 TForm1 = class(TForm) 28 Button1: TButton; 29 Button2: TButton; 30 procedure Button1Click(Sender: TObject); 31 procedure Button2Click(Sender: TObject); 32 private 33 Procedure MyCreateButton(AOwner:TWinControl; AClass:TSysButton); //AClass:TMyButton 这个也可以的 34 { Private declarations } 35 public 36 { Public declarations } 37 end; 38 var 39 Form1: TForm1; 40 implementation 41 42 var 43 i:Integer=1; 44 45 {$R *.dfm} 46 { TForm1 } 47 procedure TForm1.MyCreateButton(AOwner: TWinControl ; AClass:TSysButton); //AClass:TMyButton 这个也可以的 48 var 49 vButton:TButton; 50 begin 51 vButton:=AClass.Create(AOwner); 52 vButton.Parent:=AOwner; 53 vButton.Left :=100; 54 vButton.Top :=10+30*(i-1); 55 vButton.height:=20; 56 vButton.Width:=80; 57 vButton.Name:='MyButton_'+inttostr(i); 58 Inc(i); 59 end; 60 61 procedure TForm1.Button1Click(Sender: TObject); 62 begin 63 {从这里可以看出,不管是TButton的第几代继承‘人’, 64 都能创建该继承人类型的Button出来 65 } 66 //把这个TMyCustomerButton(Tbutton第一代继承人)传过去,就能创建TMyCustomerButton类型的Button出来 67 //MyCreateButton(Self,TMyCustomerButton); //这里把类型(TMyCustomerButton)传过去了 68 //MyCreateButton(Self,TMyCustomerButton02); //这里把类型(TMyCustomerButton02)传过去了 69 MyCreateButton(Self,TMyCustomerButton03); //这里把类型(TMyCustomerButton03)传过去了 70 end; 71 72 procedure TForm1.Button2Click(Sender: TObject); 73 var 74 vButton:TMyCustomerButton; //vButton:TMyCustomerButton02; vButton:TMyCustomerButton03 75 begin 76 vButton:=TMyCustomerButton(FindComponent('MyButton_1')); //vButton:=TMyCustomerButton02(FindComponent('MyButton_1')) 77 if vButton<>nil then 78 begin 79 vButton.FStr:='FStr_MyButton_1'; //TMyCustomerButton、TMyCustomerButton02、TMyCustomerButton03都有FStr,02和03是继承来的 80 ShowMessage(vButton.FStr); 81 end; 82 end; 83 84 end.
---Unit结束
----------Form开始
1 object Form1: TForm1 2 Left = 755 3 Top = 481 4 Width = 239 5 Height = 269 6 Caption = 'Form1' 7 Color = clBtnFace 8 Font.Charset = DEFAULT_CHARSET 9 Font.Color = clWindowText 10 Font.Height = -11 11 Font.Name = 'MS Sans Serif' 12 Font.Style = [] 13 OldCreateOrder = False 14 PixelsPerInch = 96 15 TextHeight = 13 16 object Button1: TButton 17 Left = 8 18 Top = 0 19 Width = 75 20 Height = 25 21 Caption = 'Button1' 22 TabOrder = 0 23 OnClick = Button1Click 24 end 25 object Button2: TButton 26 Left = 8 27 Top = 64 28 Width = 75 29 Height = 25 30 Caption = 'Button2' 31 TabOrder = 1 32 OnClick = Button2Click 33 end 34 end
-------Form结束