Delphi 两种类的声明方法

Delphi的类声明有两种方法,一种是继承了Delphi的内建类的声明,另一种则是完全自定义的类声明。

这两种类的区别不仅在于声明程序的不同,还会影响到对象实体的内存管理。

 1 unit Unit1;
 2 
 3 interface
 4 
 5 uses
 6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 7   Dialogs, StdCtrls, Buttons;
 8 
 9 type
10   TForm1 = class(TForm)
11     btn1: TBitBtn;
12     btn2: TBitBtn;
13     procedure btn1Click(Sender: TObject);
14     procedure btn2Click(Sender: TObject);
15   p rivate
16     { Private declarations }
17   public
18     { Public declarations }
19   end;
20 
21   TMyCalss1 = class  //声明一个继承Delphi内建类的Class类
22   public
23     value1:Integer;
24     string1:string;
25   end;
26 
27   TMyClass2 = object  //声明一个完全自定义的Object类
28   public
29     value1:Integer;
30     string1:string;
31   end;
32 
33 var
34   Form1: TForm1;
35 
36 implementation
37 
38 {$R *.dfm}
39 
40 procedure TForm1.btn1Click(Sender: TObject);
41 var
42   TTest1:TMyCalss1;
43 begin
44   TTest1 := TMyCalss1.Create;  //必须使用其父类的构造方法
45 
46   TTest1.value1 := 12;
47   TTest1.string1:= '您好';
48   ShowMessage(IntToStr(TTest1.value1) + #13 + TTest1.string1);
49 
50   TTest1.Free;            //必须使用其父类的析构方法
51 
52 end;
53 
54 
55 
56 procedure TForm1.btn2Click(Sender: TObject);
57 var
58   TTest2:TMyClass2;
59 begin
60 
61   TTest2.value1 := 34;    //不必使用Create的构造方法,可以直接构造该对象的实体,并且操作该对象的成员
62   TTest2.string1 := '我好';
63   ShowMessage(IntToStr(TTest2.value1) + #13 + TTest2.string1);
64 
65 end;
66 
67 end.

posted @ 2010-02-15 13:43  猪悟能  阅读(287)  评论(0编辑  收藏  举报