zoukankan      html  css  js  c++  java
  • 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下的类实现:

     首先是一个表示人的基类,定义如下:

     1 unit Person_Cls;
      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.

    其次一个学生类继承了上面的人类,定义如下:

     1 unit Student_Cls;
     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中当你为类提供了自定义的构造函数后,系统还是会为你提供默认的构造函数。这一点需要注意。
    至于类的使用,和一般面向对象的语言是一样的,用构造函数创建对象,在必要是卸构他就行了。

  • 相关阅读:
    服务器端接受Json数据的绑定实现
    SQL 学习笔记
    asp.net mvc下的多语言方案 包含Html,Javascript和图片
    设计和创建自己的Sharepoint Site
    SharePoint类库简要总结
    TED-谷歌创始人演示谷歌眼睛
    为什么要有战争
    跨云应用部署:共享数据存储
    使用VNET-to-VNET连接Microsoft Azure国际版和中国版
    MySQL Database on Azure新功能
  • 原文地址:https://www.cnblogs.com/jijm123/p/10828893.html
Copyright © 2011-2022 走看看