继承类的构造和析构方法


1type
2TPerson=class//(Tobject)//默认是继承Tobject的
3private
4{Privatedeclarations}
5public
6Name:String;
7Tall:Integer;
8Age:Integer;
9WaistLine:integer;
10
11{Publicdeclarations}
12end;
13procedureTForm1.Button1Click(Sender:TObject);
14VAR
15APerson:Tperson;
16begin
17APerson:=Tperson.Create;//必须用继承TObject的构造方法
18APerson.Name:='涂磊';
19APerson.Tall:=159;IntToStr(APerson.Tall);
20APerson.Age:=21;
21APerson.WaistLine:=25;
22ShowMessage(APerson.Name+#13#10+'身高='+IntToStr(APerson.Tall)+#13#10+'年龄='+IntToStr(APerson.Age)
23+#13#10+'腰围='+IntToStr(APerson.WaistLine));
24APerson.Free;//必须用TObject的析构方法比Aperson.Destroy要好,因为Free彻底清除了该对象Aperson,而Destroy只是把与对象
25//属性的链接删除,并没有清除Aperson的对象实体
26end;
27
28procedureTForm1.Button2Click(Sender:TObject);
29VAR
30AHuman:THuman;
31begin
32//AHuman:=THuman.Create;//没有继承TObject的构造方法,也不能用该方法
33AHuman.Name:='涂磊';
34AHuman.Tall:=159;IntToStr(AHuman.Tall);
35AHuman.Age:=21;
36AHuman.WaistLine:=25;
37ShowMessage(AHuman.Name+#13#10+'身高='+IntToStr(AHuman.Tall)+#13#10+'年龄='+IntToStr(AHuman.Age)
38+#13#10+'腰围='+IntToStr(AHuman.WaistLine));
39//AHuman.Free;//也不能用该TObject的析构方法
40end;
41
42
字段(Field)与对象引用(object reference)的实现
1type
2THand=class(TObject)
3public
4Length:integer;
5Thick:integer;
6Color:Tcolor;
7{Publicdeclarations}
8end;
9type
10TFoot=Object
11public
12Length:integer;
13Thick:integer;
14Color:Tcolor;
15{Publicdeclarations}
16end;
17type
18TPerson=class(TObject)
19public
20LHand:THand;//Objectreference,Class的类
21LFoot:TFoot;//ObjectInstance,Object的类
22LTall:integer;//一般字段
23{Publicdeclarations}
24end;
25
26type
27TForm1=class(TForm)
28Button1:TButton;
29Label1:TLabel;
30Label2:TLabel;
31procedureButton1Click(Sender:TObject);
32private
33{Privatedeclarations}
34public
35{Publicdeclarations}
36end;
37procedureTForm1.Button1Click(Sender:TObject);
38VAR
39tulater:TPerson;
40begin
41tulater:=TPerson.Create;
42tulater.LTall:=180;
43
44tulater.LHand:=THand.Create;//对象引用(Objectreference)
45tulater.LHand.Length:=100;
46tulater.LHand.Thick:=6;
47tulater.LHand.Color:=clYellow;
48
49tulater.LFoot.Length:=130;
50tulater.LFoot.Thick:=8;
51tulater.LFoot.Color:=clBlue;
52label1.Color:=tulater.LHand.Color;
53label2.Color:=tulater.LFoot.Color;
54ShowMessage(
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);
62tulater.LHand.Free;//对象解除引用
63tulater.Free;
64end;
65
66 优质内容筛选与推荐>>
1、第四次作业:文法和语言总结与梳理
2、【计算机网络】-传输层-拥塞控制
3、PVE手册资料
4、优秀java博客
5、Vue混入的详解


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn