现在的位置: 首页 > 综合 > 正文

Delphi的类与继承

2012年05月22日 ⁄ 综合 ⁄ 共 3929字 ⁄ 字号 评论关闭

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

   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.

抱歉!评论已关闭.