• 网络学院
  • IT资讯
  • 操作系统
  • 网络技术
  • 软件应用
  • 办公软件
  • 编程技术
  • 网站架设
  • 数据库类
  • 平面设计
  • 多媒体类
  • 游戏资讯
  • 教学论文
  • 认证考试
Delphi学习:OOP
  站点:
  • 首 页
  • 最新软件
  • 文章教程
  • 国内软件
  • 国外软件
  • 绿色软件
  • 源码下载
  • 字体下载
Delphi学习:OOP 中的双刃剑
软件发布 Delphi学习:OOP 中的双刃剑
网络软件 系统工具 应用软件 联络聊天 图形图像 多媒体类 行业软件 游戏娱乐 编程开发 安全相关 教育教学 数码软件 绿软下载
热门软件: QQ 瑞星 pplive e话通 木马克星 千千静听 office2000 五笔字根 Photoshop 视频分割
返回文章教程首页 >> 编程技术 >> Delphi教程 >> Delphi学习:OOP 中的双刃剑

Delphi学习:OOP 中的双刃剑

添加时间: 2006-2-25 5:11:41  作者: Delphi教程  阅读次数:60   来源: http://www.d9soft.com

       
  前几天看一份非常有名的商业控件的源码,发现一个非常有趣的用法:

  Integer(xxx) := aaa;
 
  Tttt(xxx) := bbb;
 
  细细品味,发现利用这种用法往往可以收到意想不到的效果:
 
  比如:



 

  TTestRec = record
    A, B, C: Integer;
  end;
  TTestCls = class
  private
    FInner: TTestRec;
    FReadOnlyValue: Integer;
 
    function GetNewInner: PTestRec;
  public
    property Inner: TTestRec read FInner write FInner;
    property NewInner: PTestRec read GetNewInner;
    property ReadOnlyValue: Integer read FReadOnlyValue;
  end;
 
  你会发现,直接的你是改不了 aTestCls.Inner.A 的(编译时 delphi 直接报错,因为 delphi 7 中两个 recode 赋值是 copy memory 而不是简单的“传址”!
 

procedure TForm1.Button1Click(Sender: TObject);
begin
  with TTestCls.Create do
  try
//    Inner.A := 10;
    Caption := TButton(Sender).Caption + ' A := ' + IntToStr(Inner.A);
  finally
    Free;
  end;
end;
 
  可是,如果我们知道在访问这个 Inner 时 delphi 在编译直接 FInner 的地址,那么,结合上面那种有趣的用法:
 

procedure TForm1.Button3Click(Sender: TObject);
var
  p: PInteger;
begin
  with TTestCls.Create do
  try
    p := @(Inner.A);
    Integer(p^) := 100;
    Caption := TButton(Sender).Caption + ' A := ' + IntToStr(Inner.A);
  finally
    Free;
  end;
end;
  更进一步,利用指针竟然可以突破 oo 对 private 的保护:
 

procedure TForm1.Button4Click(Sender: TObject);
var
  p: PInteger;
begin
  with TTestCls.Create do
  try
    p := @(ReadOnlyValue);
    Integer(p^) := 1000;

    Caption := TButton(Sender).Caption + ' ReadOnlyValue := ' + IntToStr(ReadOnlyValue);

 



  finally
    Free;
  end;
end;

 
  至于“踩过界”那更不在话下:
 

procedure TForm1.Button5Click(Sender: TObject);
var
  p1, p2: PInteger;
begin
  with TTestCls.Create do
  try
    p1 := @(Inner.A);
    // 内存中 FInner 与 FReadOnlyValue 其实只差 TTestRec 大小个字节
    Integer(p2) := Integer(p1) + SizeOf(TTestRec);
    Integer(p2^) := 100;

    Caption := TButton(Sender).Caption + ' ReadOnlyValue := ' + IntToStr(ReadOnlyValue);
  finally
    Free;
  end;
end;
 
  当然,指针不但可以破坏 oo,也能使您的代码更加的 oo:
 

  TTestRec = record
    A, B, C: Integer;
  end;
  PTestRec = ^TTestRec;
 
  TTestCls = class
  private
    FInner: TTestRec;
    FReadOnlyValue: Integer;
 
    function GetNewInner: PTestRec;
  public
    property Inner: TTestRec read FInner write FInner;
    property NewInner: PTestRec read GetNewInner;
    property ReadOnlyValue: Integer read FReadOnlyValue;
  end;
 
procedure TForm1.Button2Click(Sender: TObject);
begin
  with TTestCls.Create do
  try
   
NewInner.A := 10;
    Caption := TButton(Sender).Caption + ' A := ' + IntToStr(Inner.A);
  finally
    Free;
  end;
end;
 
  看看现实中的非 oo 的代码:
 
  利用“指针方案”把 Txxx 改成 Pxxx 后竟然对原来的代码一点影响都没有,而使之更加的 oo

 

上下文章:

 

上一篇文章: Delphi学习:查句柄知多少 下一篇文章: 在 NT内核的操作系统上实现系统关闭

相关文章:

  • 学习Ruby-Web程序开发的10条理由
  • Oracle指导:Oracle学习笔记
  • Oracle专家高级编程学习笔记[1]
  • 我的学习总结:Oracle软件结构(1)
  • Oracle10g学习手册1:证书的验证下

相关软件:

  • Turbo C for Windows 集成实验与学习环境 V6.11
  • InfoPower for Delphi 5 V2000.17
  • 汇编语言学习工具 3.6
  • EnglishField英语学习软件 7.8
  • 限时英语(初中英语学习软件) 6.4
  • 英语学习王 V2007 Build 1221 真人发音版

 

快速导航

  • 网络学院
  • 精品汇聚
  • 字体下载
  • 教程下载
  • ASP源码
  • PHP源码
  • Net源码
  • JSP 源码

编程技术分类导航

  • ASP & ASP.NET教程
  • PHP教程
  • JSP教程
  • C/C++教程
  • VB & VB.NET教程
  • VC教程
  • Delphi教程
  • BCB教程
  • VFP教程
  • PB教程
  • JAVA教程
  • XML教程
  • C#教程
  • CGI教程

本类经典文章推荐

  • Delphi的两个实用技巧(1)播放Flash
  • Delphi的两个实用技巧(2)巧用Wind...
  • delphi实例编程之--制作可随处拖放...
  • 关于VisiBroker For Delphi的使用...
  • 关于VisiBroker For Delphi的使用...
  • 关于VisiBroker For Delphi的使用...
  • 在线播放器DIY
  • Delphi让你发送Flash电子邮件
  • 在窗口标题区添加按钮
  • 用Delphi 6编程实现自动标注汉语拼...

Delphi教程阅读排行

  • Delphi7从入门到精通之历数Delphi...
  • Delphi的两个实用技巧(1)播放Flash
  • Delphi7从入门到精通之认识Delphi...
  • delphi实例编程之--制作可随处拖放...
  • Delphi控件,我们也可以(1)
  • Delphi的两个实用技巧(2)巧用Wind...
  • Spcomm串口控件的例程
  • 在线播放器DIY
  • Delphi控件,我们也可以(2)
  • 用Delphi + DirectX开发简单RPG游...

编程技术阅读总排行

  • VB入门教程之一
  • Java连接数据库实例
  • VC++之List Box/Check List Box控...
  • 第二章 PowerBuilder 入门之创建新...
  • VC++ List Ctrl控件
  • 第一章 什么是PowerBuilder
  • VC++ Combo Box/Combo Box Ex控件
  • 学C++不得不看的一篇文章
  • VB入门教程之二
  • VC++之Button控件

广告位置

字母检索 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 回到顶部

关于我们 | 版权声明 | 免责条款 | 广告联系 | 软件发布 | 下载帮助 | 下载排行 | 网站地图 | 特别鸣谢 | 友情连接

copyright; 2005-2008 D9soft.com 第九软件网 版权所有