1
type
2
TPerson = class//(Tobject) //默认是继承 Tobject的
3
private
4
{ Private declarations }
5
public
6
Name: String;
7
Tall: Integer;
8
Age: Integer;
9
WaistLine: integer;
10
11
{ Public declarations }
12
end;
13
procedure TForm1.Button1Click(Sender: TObject);
14
VAR
15
APerson: Tperson;
16
begin
17
APerson := Tperson.Create; //必须用继承 TObject的构造方法
18
APerson.Name := '涂磊';
19
APerson.Tall := 159; IntToStr(APerson.Tall);
20
APerson.Age := 21;
21
APerson.WaistLine := 25;
22
ShowMessage(APerson.Name + #13#10 + '身高= ' + IntToStr(APerson.Tall) + #13#10 + '年龄= ' + IntToStr(APerson.Age)
23
+ #13#10 + '腰围= ' + IntToStr(APerson.WaistLine));
24
APerson.Free; //必须用 TObject的析构方法 比Aperson.Destroy要好,因为Free 彻底清除了该对象Aperson,而Destroy只是把与对象
25
//属性的 链接删除,并没有清除Aperson的对象实体
26
end;
27
28
procedure TForm1.Button2Click(Sender: TObject);
29
VAR
30
AHuman: THuman;
31
begin
32
//AHuman := THuman.Create; //没有继承 TObject的构造方法,也不能用该方法
33
AHuman.Name := '涂磊';
34
AHuman.Tall := 159; IntToStr(AHuman.Tall);
35
AHuman.Age := 21;
36
AHuman.WaistLine := 25;
37
ShowMessage(AHuman.Name + #13#10 + '身高= ' + IntToStr(AHuman.Tall) + #13#10 + '年龄= ' + IntToStr(AHuman.Age)
38
+ #13#10 + '腰围= ' + IntToStr(AHuman.WaistLine));
39
// AHuman.Free; //也不能用该TObject的析构方法
40
end;
41
42

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

字段(Field)与对象引用(object reference)的实现
1
type
2
THand = class(TObject)
3
public
4
Length: integer;
5
Thick: integer;
6
Color: Tcolor;
7
{ Public declarations }
8
end;
9
type
10
TFoot = Object
11
public
12
Length: integer;
13
Thick: integer;
14
Color: Tcolor;
15
{ Public declarations }
16
end;
17
type
18
TPerson = class(TObject)
19
public
20
LHand: THand; //Object reference ,Class的类
21
LFoot: TFoot; //Object Instance ,Object的类
22
LTall: integer; //一般字段
23
{ Public declarations }
24
end;
25
26
type
27
TForm1 = class(TForm)
28
Button1: TButton;
29
Label1: TLabel;
30
Label2: TLabel;
31
procedure Button1Click(Sender: TObject);
32
private
33
{ Private declarations }
34
public
35
{ Public declarations }
36
end;
37
procedure TForm1.Button1Click(Sender: TObject);
38
VAR
39
tulater: TPerson;
40
begin
41
tulater := TPerson.Create;
42
tulater.LTall := 180;
43
44
tulater.LHand := THand.Create; //对象引用(Object reference )
45
tulater.LHand.Length := 100;
46
tulater.LHand.Thick := 6;
47
tulater.LHand.Color := clYellow;
48
49
tulater.LFoot.Length := 130;
50
tulater.LFoot.Thick := 8;
51
tulater.LFoot.Color := clBlue;
52
label1.Color := tulater.LHand.Color;
53
label2.Color := tulater.LFoot.Color;
54
ShowMessage(
55
' tulater ' + #13 +
56
' 身高 = ' + IntToStr(tulater.LTall) + #13 +
57
' 手长 = ' + IntToStr(tulater.LHand.Length) + #13 +
58
' 手粗 = ' + IntToStr(tulater.LHand.Thick) + #13 +
59
' 脚长 = ' + IntToStr(tulater.LFoot.Length) + #13 +
60
' 脚粗 = ' + IntToStr(tulater.LFoot.Color) + #13
61
);
62
tulater.LHand.Free; //对象解除引用
63
tulater.Free;
64
end;
65
66

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66
