inherited使用举例
Iherited 是一個保留字,在用在子類的(Method)的實現區里,當要設定子類某個方法的
實現內容,但這個方法實現區所包含的代碼只會比父類所定義的實現內容多出一些代碼,
而原本的部分仍然要延用時,就可以在類方法的實現區中用 Iherited 這個保留字
后面加上父類的成員函數的標識符(Identifier) ,并且給予適當參數.
而且,Iherited并非只能在override 的方法實現里,而且在子類設定的任何方法里使用
而且能調用父類任何成員函數.
1unitUnit1;
2
3interface
4
5uses
6Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
7Dialogs,StdCtrls;
8type
9TPerson=class(TObject)
10public
11CONSTRUCTORCreate;
12FUNCTIONSwim:String;
13{Publicdeclarations}
14end;
15type
16TET=class(TPerson)
17public
18Name:String;
19PROCEDURESwim;
20PROCEDURESwum;
21{Publicdeclarations}
22end;
23type
24TForm1=class(TForm)
25Button1:TButton;
26procedureButton1Click(Sender:TObject);
27private
28{Privatedeclarations}
29public
30{Publicdeclarations}
31end;
32
33var
34Form1:TForm1;
35
36implementation
37
38PROCEDURETET.Swum;
39BEGIN
40ShowMessage('根據之前的資料'+#13+#13+'剛才');//+inheritedSwim);
41
42end;
43
44PROCEDURETET.Swim;
45BEGIN
46ShowMessage(Self.ClassName+'的Create'+#13+#13);//+inheritedSwim);
47
48end;
49
50FUNCTIONTPerson.Swim:STRING;
51BEGIN
52result:='你游'+InputBox(Self.ClassName+'游泳池問卷','你游什么方式','蛙游')+'啊!';
53ShowMessage('問卷填好了!');
54end;
55
56CONSTRUCTORTPerson.Create;
57BEGIN
58INHERITED;
59ShowMessage('執行'+Self.ClassName+'的Create'+#13+#13+'歡迎光臨Fish游泳池');
60end;
61
62{$R*.dfm}
63
64procedureTForm1.Button1Click(Sender:TObject);
65VAR
66aPerson:TPerson;
67theEt:Tet;
68begin
69aPerson:=TPerson.Create;
70aPerson.Swim;
71aPerson.Free;
72theEt:=TET.Create;
73theEt.Name:='外星人ET';
74theEt.Swim;
75theEt.Swum;
76theEt.Free;
77end;
78
79end.
80
81
總結
以上是生活随笔為你收集整理的inherited使用举例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 区块链共识机制:POW、POA、POS、
- 下一篇: Oracle init.ora常用配置详