既然已经做出了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的类与继承(VB与delphi比较)的更多相关文章

  1. Delphi的类与继承

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

  2. C#创建COM组件供VB,PB,Delphi调用

    1  COM组件概述 COM是微软公司为了计算机工业的软件生产更加符合人类的行为方式开发的一种新的软件开发技术.在COM构架下,人们可以开发出各种各样的功能专一的组件,然后将它们按照需要组合起来,构成 ...

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

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

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

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

  5. Java类的继承与多态特性-入门笔记

    相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...

  6. Delphi 遍历类中的属性

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

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

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

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

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

  9. (转)Java:类与继承

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

随机推荐

  1. R语言画云字图

    install.packages('wordcloud') library(wordcloud) colors=c('red','blue','green','yellow','purple') da ...

  2. SQL Server数据库SP命令祥解

    1.数据库: (1)sp_helpdb:报告有关指定数据库或所有数据库的信息.例:sp_helpdb   --显示所有数据库信息(名称.大小等)例:sp_helpdb Recruitment   -- ...

  3. Windows 安装JRuby 生成 war 到 tomcat 运行

    Windows安装JRuby Rails 直接下载 JRuby,不装 Ruby. http://jruby.org/download 该安装包可以配好环境变量 %JRUBY_HOME% 等 安装 bu ...

  4. eclipse建立springMVC 简单项目

    http://jinnianshilongnian.iteye.com/blog/1594806 如何通过eclipse建立springMVC的简单项目,现在简单介绍一下. 工具/原料   eclip ...

  5. Autoit3 获取WinForm下的ToolTip

    相比Autohotkey,在我看来,Autoit最实用的就是对于WinForm Application的良好支持 然而,要想将鼠标放在WinForm的ToolTip上,简直无异于自己把自己举起来,故而 ...

  6. static修饰符

    static修饰符表示静态的,可修饰字段.方法.内部类,其修饰的成员属于类,也就是说static修饰的资源属于类级别,而不是对象级别. static的正真作用:用来区别字段,方法,内部类,初始化代码块 ...

  7. html显示缩略小图 无失真图片

    <html> <head> <title>我的图片处理</title> <style type="text/css"> ...

  8. Java的Json解析包FastJson使用

    阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser. ...

  9. Windows+Caffe+VS2013+python接口配置过程

    前段时间在笔记本上配置了Caffe框架,中间过程曲曲折折,但由于懒没有将详细过程总结下来,这两天又在一台配置较高的台式机上配置了Caffe,配置时便非常后悔当初没有写到博客中去,现已配置好Caffe, ...

  10. Xcode工作区间xxxx.xcworkspace不包含xxxx.xcodeproj

    一.问题描述 项目用到cocoapods管理第三方框架,所以需要打开xxxx.xcworkspace,Pods正常显示,但xxxx.xcodeproj显示红色,不包含xxxx.xcodeproj并且无 ...