既然已经做出了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下的类实现:

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

  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:= ; Self.tall:=; Self.weight:=;
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>) then
Self.year :=
else
Self.year :=Value ;
end;
function Person.get_tall :integer;
begin
result:=Self.tall ;
end;
procedure Person.set_tall (Value:integer);
begin
if(Value>) then
Self.tall:=
else
Self.tall:=Value ;
end; function Person.get_weight :integer;
begin
result:=Self.weight ;
end;
procedure Person.set_weight(Value:integer);
begin
if(Value>) then
Self.weight:=
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.

以上就是这两个简单的类定义,很好的说明了delphi中类和派生类的关系。
需要补充的一点就是,delphi中当你为类提供了自定义的构造函数后,系统还是会为你提供默认的构造函数。这一点需要注意。
至于类的使用,和一般面向对象的语言是一样的,用构造函数创建对象,在必要是卸构他就行了。

Delphi的类与继承的更多相关文章

  1. 转:Delphi的类与继承(VB与delphi比较)

    既然已经做出了com程序用delphi来开发的决定,那当然就要对delphi进行一些深入的了解.有人说delphi是一个用控件堆砌起来的工具,和vb没什么两样:也有人说dephi实际上是面向过程的,他 ...

  2. QMetaObject感觉跟Delphi的类之类有一拼,好好学一下

    提供了一堆原来C++没有的功能,比如反射什么的...但是可能还是没有Delphi的类之类更强,因为类之类可以“创建类”.可惜我学艺不精,对“类之类”也没有完全学会.先留个爪,有空把两个东西都好好学学, ...

  3. DELPHI学习---类和对象(五篇)

    Classes and objects(类和对象) 类(或者类类型)定义了一个结构,它包括字段(也称为域).方法和属性:类的实例叫做对象:类的字段.方法和属性被称为它的部件(components)或成 ...

  4. Delphi 遍历类中的属性

    http://blog.csdn.net/easyboot/article/details/8004954 Delphi 遍历类中的属性 标签: delphistringbuttonclassform ...

  5. UML类图(上):类、继承和实现

    面向对象设计 对于一个程序员来说,在工作的开始阶段通常都是别人把东西设计好,你来做.伴随着个人的成长,这个过程将慢慢变成自己设计一部分功能来实现,自己实现.如果要自己设计,无论是给自己看,还是给别人看 ...

  6. 【Python五篇慢慢弹(5)】类的继承案例解析,python相关知识延伸

    类的继承案例解析,python相关知识延伸 作者:白宁超 2016年10月10日22:36:57 摘要:继<快速上手学python>一文之后,笔者又将python官方文档认真学习下.官方给 ...

  7. (转)Java:类与继承

    原文地址: http://www.cnblogs.com/dolphin0520/p/3803432.html 对于面向对象的程序设计语言来说,类毫无疑问是其最重要的基础.抽象.封装.继承.多态这四大 ...

  8. iBatis.net 类的继承extends和懒加载

    <resultMaps> <resultMap id="FullResultMap" class="t_c_team_member_permission ...

  9. python 类定义 继承

    0 前言 系统:win7 64bit IDE : python(x,y) 2.7.6.1 IDE集成的解释器:Python 2.7.6 (default, Nov 10 2013, 19:24:18) ...

随机推荐

  1. 2019.9.16 linux安装软件lamp

    2019/9/16 Linux软件安装 方式:yum/rpm/源码安装 yum安装: yum 是通过分析RPM的包头数据后,根据各软件的相关性制作出属性相对应的解决方案,然后可以自动处理软件的相依属性 ...

  2. python的并发模块concurrent

    Python3.2开始,标准库为我们提供了concurrent.futures模块,它提供了ThreadPoolExecutor和ProcessPoolExecutor两个类,实现了对threadin ...

  3. 移动端h5模拟长按事件

    为啥写这篇文章 最近接了个需求,要求长按某个标签显示删除一个悬浮的删除按钮.这个需求其实在app上很常见,但是在移动端h5中,我们没有长按的事件,所以就需要自己模拟这个事件了. 大概效果如下: ps: ...

  4. thinkphp一般数据库操作

    引入命名空间 插入 更新 查询 删除 一些支持命令行的操作 清空操作 分库操作 分库相关配置---在config.php中进行 使用: 参数绑定 占位符绑定 第一句后半拉

  5. eclipse安装hibernate tools插件

    第一步:在eclipse菜单中选择Help>Install New Software,弹出安装插件的窗口,如图: 在Work with:后面输入http://download.jboss.org ...

  6. iOS中延迟执行和取消的几种方式

    公用延迟执行的方法: - (void)delayMethod { NSLog(@"delayMethodEnd"); } 方法一.performSelector 方法 1.延迟执行 ...

  7. JAVA笔记27-正则表达式(RegularExpressions)

    正则表达式是字符串的处理利器. 用途:字符串匹配(字符匹配).字符串查找.字符串替换 例如:IP地址是否正确.从网页中揪出email地址(如垃圾邮件).从网页中揪出链接等 涉及到的类:java.lan ...

  8. DOM例子小结(一)

    一.点击按钮切换图片 核心思路: 1.首先获取元素 2.为元素添加点击事件 3.当事件被触发时运行代码 <!DOCTYPE html> <html lang="en&quo ...

  9. node.js入门学习(六)--express

    1.官网:http://expressjs.com/ 中文:http://www.expressjs.com.cn/ 2.HelloWorld 1)mkdir node-express-demo 2) ...

  10. HTML5 大文件断点续传完整思路整理

    需求: 支持大文件批量上传(20G)和下载,同时需要保证上传期间用户电脑不出现卡死等体验: 内网百兆网络上传速度为12MB/S 服务器内存占用低 支持文件夹上传,文件夹中的文件数量达到1万个以上,且包 ...