All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
新手入门:PHP网站开发中常见问题汇总_php资料_编程技术
GDI+ 绘制折线图分析网站流量
常用的 MS SQL Server 数据修复命令_[SQL Server教程]
PHP动态网页编程:include()与require()的对比_php资料_编程技术
delphi组件的基本操作
使用PHP制作动态网页过程中的五个技巧_php资料_编程技术
C#网络应用编程基础练习题与答案(一)_[Asp.Net教程]
用PHP连接Oracle数据库_php资料_编程技术
提高asp.net网站性能的7个方法_[Asp.Net教程]
高效交换XML文档_[XML教程]
Reading number is top 10 pictures
Nikon microscopic photography of the first three
Shandong jinan is about to dismantle a one hundred-year history of the building
抗日又出雷剧情
Ashlynn Brooke photograph of a group2
囚犯暴乱了咋办?
西游四格漫画(六)
为什么别人说你是疯子
Female model behind the bitterness, often being overcharged2
美丽的桂林风光1
你的钱干净吗?
Download software ranking
linux初级教程
C语言教程TXT
Ashlynn Video1
Sora aoi, the nurse, uniform ,nursing assistant
Tram sex maniac 2 (H) rar bag8
株洲本地在线棋牌游戏
I'm come from Beijing1
Kung fu panda - the secret of the teacher
Desire a peach blossom
Sora aoi, the maid, students' uniforms
aaa published in(发表于) 2014/7/19 0:12:46 Edit(编辑)
delphi类运算符is和as的使用实例

delphi类运算符is和as的使用实例

delphi类运算符is和as的使用实例|方法

在程序运行期间,可以使用Is 运算符和As 运算符来进行类信息检测和类型转换,通常也把这两个运算符称为运行时类型信息(RTTI:Runtime Type Information)运算符。

1.Is 运算符

Is 运算符用来检测一个对象在运行时的类的类型,具体形式如下:

object is class

如果返回值为True,那么对象Object 是类Class 或者是类Class 派生类的一个实例。如果对象为nil,返回值则为False。

2.As 运算符

As 运算符用来进行类型转换检测的,具体形式如下:

object as class
返回值为Object 的一个为Class 类型的引用。在运行期间,Object 必须是与Class 类兼容的一个类的对象或nil。通常为了避免类型不兼容,可以使用Is 运算符来进行类型判断。

下面的例子对Is 运算符和As 运算符进行了说明:

program Project1;

{$APPTYPE CONSOLE}

type

TPerson = class //人类

public

Name:string; //姓名

end;

TEmployee = class(TPerson) //顾客类

public

DeptName:string; //地址名称

procedure Infor; //只有子类具有"显示信息"的方法

end;

TCustomer = class(TPerson) //顾客类

public

DeptName:String;

procedure Infor;

end;

procedure TEmployee.Infor;

begin

Writeln(’姓名:’,Name,’; 部门名称:’,DeptName);

end;

procedure TCustomer.Infor;

begin

Writeln(’姓名:’,Name,’; 部门名称:’,DeptName);

end;

var

P1: TPerson; //声明一个人类的变量

E1: TEmployee; //声明一个顾客类的变量

begin

P1:=TPerson.Create; //P1 为父类的对象

P1.Name:=’张三’;

if P1 is TCustomer then (P1 as TCustomer).DeptName:=’人事部’ ;

if P1 is TCustomer then (P1 as TCustomer).Infor;

P1.Free;

P1:=TCustomer.Create; //P1 为子类的对象

P1.Name:=’冯六’;

if P1 is TCustomer then (P1 as TCustomer).DeptName:=’人事部’ ;

if P1 is TCustomer then (P1 as TCustomer).Infor;

P1.Free;

E1:=TEmployee.Create; //E1 为子类的对象

E1.Name:=’高七’;

if E1 is TEmployee then (E1 as TEmployee).DeptName:=’公关部’;

if E1 is TEmployee then (E1 as TEmployee).Infor;

E1.Free;

Readln;

end.

运行结果如下:

姓名:冯六; 部门名称:人事部

姓名:高七; 部门名称:公关部

这里有几点需要说明:

• 当P1 调用TPerson 类的构造函数的时候,创建的是一个TPerson 的对象,不可以调用子类TCustomer 中特有的过程Infor,也不可以对子类中的特有属性DeptName 进行操作。在调用时要用Is运算符来判断P1 是否为TCustomer 类的对象。

• TPerson 类的变量P1 被“创建”了两次,由于前后两次创建的类型不同,所以Is 运算符判断的结果不同。第2 次“P1 is TCustomer”返回的是True,然后进行类型转换并访问DeptName 属性和调用Infor 过程。




添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.