Delphi的类与继承
????? 既然已經做出了com程序用delphi來開發的決定,那當然就要對delphi進行一些深入的了解。有人說delphi是一個用控件堆砌起來的工具,和vb沒什么兩樣;也有人說dephi實際上是面向過程的,他的面向對象并不徹底。實際生活中持這兩種觀點的人不在少數,就拿我認識的一個非常好的程序員來說吧,他很早就開始用vb,到后來接觸到delphi,并且用delphi也開發了不少程序,但吃驚的是這些程序全都是面向過程的。我并沒有貶低這種做法的意思,我想說的是:好的delphi控件+好的編程規范+優秀的vcl 也能造就很多實用的程序。況且他也是位非常好的vb程序員,俗話說僅能使用vb還顯得很初級,但用vb編寫出非常穩定可靠的企業應用,那就非常優秀。但這樣的人很少很少,為什么?因為企業級應用往往要求較高的可靠性和可擴展性,其中可靠性就是vb的弱點,很多時候為了實現一個稍微復雜的應用,vb就需要大量的第三方控件,大量的windows api調用(這些api在后期很難閱讀與維護),大量的看似相互獨立但無須申明就可以四處調用的模塊,再加上一堆的全局變量,最終的局面是程序越大,程序員的腦袋就越大。但有人就說了,那vb不也有類嗎?是啊,它是有,但一切面向對象的思想都在這個所謂的類上找不到影子:什么重載,虛函數,重寫,繼承...等等。而這些delphi都有。當然了,vb畢竟是一個易學易用的語言,從一開始,微軟就沒說過他是一個全面向對象的語言(這也包括最終的vb6.1)。再有就是可擴展性,用vb開發的程序一般可擴展性都比較低,這是不爭的事實,我想就不用多說了,誰身在其中,誰自然最清楚。vb中好的程序員,多半是具有很高的編程技巧,能將vb的弱點降到最低的人,他們往往很早就接觸vb,感情釋然,積累也很豐富,放不下了。而我呢,又是個木瓜腦袋,是很難達到那些牛人的水平了,所以還是決定徹底放棄vb去選擇一個優秀的開發工具,那就是delphi。
??? 從最初的c語言到vb,又從vb 到delphi,再從delphi到c#,最后又回過頭來看delphi。雖說delphi的面向對象特性沒有c#那樣徹底,但delphi至少實現了80%-90%的oop特性。不廢話了,先來看兩個delphi下的類實現:
?首先是一個表示人的基類,定義如下:
??2?
??3?interface
??4?
??5?type
??6????Person=class???????//基類
??7????private
??8??????name:string;???????//私有變量(姓名,性別,身高,體重)
??9??????sex:string;
?10??????year:integer;
?11??????tall:integer;
?12??????weight:integer;
?13??????function?get_name:string;???????????//聲明返回和設置屬性的函數
?14??????procedure?set_name(Value:string);???//以下同
?15??????function?get_sex:string;
?16??????procedure?set_sex(Value:string);
?17??????function?get_year:integer;
?18??????procedure?set_year(Value:integer);
?19??????function?get_tall:integer;
?20??????procedure?set_tall(Value:integer);
?21??????function?get_weight:integer;
?22??????procedure?set_weight(Value:integer);
?23?
?24????public
?25??????constructor?Create(Name:string;Sex:string;Year:integer;Tall:integer;Weight:integer);overload;
?26??????constructor?Create(Name:string;Sex:string);overload;????//重載構造函數,注意一定要使用關鍵字:overload
?27??????property?_name:string?read?get_name?write?set_name;?????//為類定義屬性,以便在外部可以訪問
?28??????property?_sex:string?read?get_sex?write?set_sex;?????????//其中read表示可讀,后面緊跟一個返回該屬性值的函數名
?29??????property?_year:integer?read?get_year?write?set_year;?????//write?表示可寫,后面緊跟一個設置該屬性值的函數名
?30??????property?_tall:integer?read?get_tall?write?set_tall;
?31??????property?_weight:integer?read?get_weight?write?set_weight;
?32?
?33?end;
?34?
?35?implementation
?36?//構造函數的實現
?37?constructor?Person.Create(Name:string;Sex:string;Year:integer;Tall:integer;Weight:integer);
?38?begin
?39?????Self.name:=Name?;?Self.sex:=Sex?;?Self.year:=Year?;?Self.tall:=Tall;?Self.weight:=Weight?;
?40?
?41?end;
?42?constructor?Person.Create(Name:string;Sex:string);
?43?begin
?44????Self.name:=Name?;?Self.sex:=Sex?;?Self.year:=0?;?Self.tall:=0;?Self.weight:=0;
?45?end;
?46?
?47?//類屬性的內部實現??請與c#作比較:get{},set{}
?48?function?Person.get_name:string;
?49?begin
?50?????result:=Self.name?;
?51?end;
?52?procedure?Person.set_name(Value:string);
?53?begin
?54?????if?(Value<>'')?then
?55?????????name?:=Value?;
?56?end;
?57?function?Person.get_sex?:string;
?58?begin
?59????result:=Self.sex;
?60?end;
?61?procedure?Person.set_sex(Value:string);
?62?begin
?63????if((Value<>'female')?and?(Value<>'male'))?then
?64???????Self.sex?:='male'
?65????else
?66???????Self.sex?:=Value;
?67?
?68?end;
?69?function?Person.get_year?:integer;
?70?begin
?71?????result:=Self.year?;
?72?end;
?73?procedure?Person.set_year(Value:integer);
?74?begin
?75?????if(Value>200)?then
?76???????Self.year?:=0
?77?????else
?78???????Self.year?:=Value?;
?79?end;
?80?function?Person.get_tall??:integer;
?81?begin
?82?????result:=Self.tall??;
?83?end;
?84?procedure?Person.set_tall?(Value:integer);
?85?begin
?86?????if(Value>300)?then
?87???????Self.tall:=0
?88?????else
?89???????Self.tall:=Value?;
?90?end;
?91?
?92?function?Person.get_weight???:integer;
?93?begin
?94?????result:=Self.weight???;
?95?end;
?96?procedure?Person.set_weight(Value:integer);
?97?begin
?98?????if(Value>1000)?then
?99???????Self.weight:=0
100?????else
101???????Self.weight:=Value?;
102?end;
103?
104?end.
?
其次一個學生類繼承了上面的人類,定義如下:
?2?
?3?interface
?4???uses?Person_Cls;
?5???type
?6???????Student=Class(Person)
?7???????private
?8?????????stCode:string;??//學號
?9?????????department:string;?//學院?(大學),學校名稱(其他)
10?????????classGrade:string;?//班級
11?????????function?get_stCode:string;
12?????????function?get_department:string;
13?????????function?get_classGrade:string;
14?
15???????public
16?????????//構造函數定義
17?????????constructor?Create(s_name:string;s_sex:string;st_code:string;st_dt:string;st_clg:string);
18?????????property?_stCode:string?read?get_stCode;??????????????//定義只讀屬性
19?????????property?_department:string?read?get_department;
20?????????property?_classGrade:string?read?get_classGrade;
21?
22???end;
23?
24?implementation
25?constructor?Student.Create(s_name:string;s_sex:string;st_code:string;st_dt:string;st_clg:string);
26?begin
27?????inherited?Create(s_name,s_sex);??//注意在此使用inherited關鍵字調用基類的構造函數,并向基類person傳遞
28??????????????????????????????????????//參數。在c#中可以使用base指定參數列表,在delphi中要使用inherited顯示說明
29?????Self.stCode?:=st_code;?Self.department:=st_dt?;?Self.classGrade:=st_clg?;
30?
31?end;
32?//只讀屬性的內部實現
33?function?Student.get_stCode?:string?;
34?begin
35?????result:=Self.stCode?;
36?end;
37?function?Student.get_department?:string?;
38?begin
39?????result:=Self.classGrade?;
40?end;
41?function?Student.get_classGrade?:string;
42?begin
43?????result:=Self.classGrade?;
44?end;
45?end.
以上就是這兩個簡單的類定義,很好的說明了delphi中類和派生類的關系。
需要補充的一點就是,delphi中當你為類提供了自定義的構造函數后,系統還是會為你提供默認的構造函數。這一點需要注意。
至于類的使用,和一般面向對象的語言是一樣的,用構造函數創建對象,在必要是卸構他就行了。
轉載于:https://www.cnblogs.com/Hi-ILoveFeng/archive/2006/06/15/426180.html
總結
以上是生活随笔為你收集整理的Delphi的类与继承的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用HTML文件作为中转生成WORD文档
- 下一篇: SoapRpcMethodAttribu