delphi 类与继承
提倡采用面向对象的方法开发DELPHI应用程序。
定义人类:
unit Person_Cls;
interface
type
Person=class //基类
private
name:string; //私有变量(姓名,性别,身高,体重)
sex:string;
year:integer;
tall:integer;
weight:integer;
function get_name:string; //声明返回和设置属性的函数
procedure set_name(Value:string); //以下同
function get_sex:string;
procedure set_sex(Value:string);
function get_year:integer;
procedure set_year(Value:integer);
function get_tall:integer;
procedure set_tall(Value:integer);
function get_weight:integer;
procedure set_weight(Value:integer);
public
constructor Create(Name:string;Sex:string;Year:integer;Tall:integer;Weight:integer);overload;
constructor Create(Name:string;Sex:string);overload; //重载构造函数,注意一定要使用关键字:overload
property _name:string read get_name write set_name; //为类定义属性,以便在外部可以访问
property _sex:string read get_sex write set_sex; //其中read表示可读,后面紧跟一个返回该属性值的函数名
property _year:integer read get_year write set_year; //write 表示可写,后面紧跟一个设置该属性值的函数名
property _tall:integer read get_tall write set_tall;
property _weight:integer read get_weight write set_weight;
end;
implementation
//构造函数的实现
constructor Person.Create(Name:string;Sex:string;Year:integer;Tall:integer;Weight:integer);
begin
Self.name:=Name ; Self.sex:=Sex ; Self.year:=Year ; Self.tall:=Tall; Self.weight:=Weight ;
end;
constructor Person.Create(Name:string;Sex:string);
begin
Self.name:=Name ; Self.sex:=Sex ; Self.year:=0 ; Self.tall:=0; Self.weight:=0;
end;
//类属性的内部实现 请与c#作比较:get{},set{}
function Person.get_name:string;
begin
result:=Self.name ;
end;
procedure Person.set_name(Value:string);
begin
if (Value<>'') then
name :=Value ;
end;
function Person.get_sex :string;
begin
result:=Self.sex;
end;
procedure Person.set_sex(Value:string);
begin
if((Value<>'female') and (Value<>'male')) then
Self.sex :='male'
else
Self.sex :=Value;
end;
function Person.get_year :integer;
begin
result:=Self.year ;
end;
procedure Person.set_year(Value:integer);
begin
if(Value>200) then
Self.year :=0
else
Self.year :=Value ;
end;
function Person.get_tall :integer;
begin
result:=Self.tall ;
end;
procedure Person.set_tall (Value:integer);
begin
if(Value>300) then
Self.tall:=0
else
Self.tall:=Value ;
end;
function Person.get_weight :integer;
begin
result:=Self.weight ;
end;
procedure Person.set_weight(Value:integer);
begin
if(Value>1000) then
Self.weight:=0
else
Self.weight:=Value ;
end;
end.
定义学生:
unit Student_Cls;
interface
uses Person_Cls;
type
Student=Class(Person)
private
stCode:string; //学号
department:string; //学院 (大学),学校名称(其他)
classGrade:string; //班级
function get_stCode:string;
function get_department:string;
function get_classGrade:string;
public
//构造函数定义
constructor Create(s_name:string;s_sex:string;st_code:string;st_dt:string;st_clg:string);
property _stCode:string read get_stCode; //定义只读属性
property _department:string read get_department;
property _classGrade:string read get_classGrade;
end;
implementation
constructor Student.Create(s_name:string;s_sex:string;st_code:string;st_dt:string;st_clg:string);
begin
inherited Create(s_name,s_sex); //注意在此使用inherited关键字调用基类的构造函数,并向基类person传递
//参数。在c#中可以使用base指定参数列表,在delphi中要使用inherited显示说明
Self.stCode :=st_code; Self.department:=st_dt ; Self.classGrade:=st_clg ;
end;
//只读属性的内部实现
function Student.get_stCode :string ;
begin
result:=Self.stCode ;
end;
function Student.get_department :string ;
begin
result:=Self.classGrade ;
end;
function Student.get_classGrade :string;
begin
result:=Self.classGrade ;
end;
end.
使用方法:
var
Form1: TForm1;
implementation
uses Student_Cls;//将类USES进来
{$R *.dfm}
procedure TForm1.Button6Click(Sender: TObject);
var
my:Student;
begin
my.Create('','男','053530051','湖南广播电视大学','05计算机应用二班');
my._name:= '梁清福';
showmessage(my._name+''+my._department);
end;
分享到:
相关推荐
Borland官方Delphi7 Vcl类属继承图
Delphi7 VCL继承关系全图.一图的方式详细介绍Delphi7 VCL继承关系。
Delphi7 VCL继承关系全图 PDF格式
Delphi7 VCL继承关系全图
Borland官方Delphi7类属继承图.rarBorland官方Delphi7类属继承图.rar
Delphi VCL 继承关系图 VCLHierarchyPoster pdf 版本,delphi 7 的。
网上收集,希望对朋友有帮助!!!
Borland官方Delphi7 Clx类属继承图
Borland官方Delphi7 Clx类属继承图.pdf
实现基于窗体的继承实例代码开发,针对窗体中显示通用内容时可参考。
最近用到窗口继承方面的东西,写了一个小示例供初学者参考
这个是本人最近编制的一个窗体继承类。程序代码很短,但很有用,解决了Delphi程序设计中窗体及其包含的控件自适应不同屏幕分辨率的问题。 即你的程序可能是在1024X768屏幕分辨率下设计的,但是需要在屏幕分辨率为...
Borland官方Delphi7 Vcl类属继承图.pdf
delphi控件继承关系浏览器 功能有限请多给建议和帮助