网页功能: 加入收藏 设为首页 网站搜索  
Delphi学习:OOP 中的双刃剑
发表日期:2004-11-03作者:lsuper[转贴] 出处:CSDN  

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

  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学习:OOP 中的双刃剑
本类热点文章
  DBGrid 应用全书
  DBGrid 应用全书
  TWebBrowser编程简述
  用户界面设计的技巧与技术
  用户界面设计的技巧与技术
  初探Delphi 7 中的插件编程
  获取主板BIOS的信息
  网卡的远程网络唤醒
  Delphi 2006简介(Dexter)
  用Delphi开发数据库程序经验三则
  Delphi面向对象编程的20条规则
  Delphi面向对象编程的20条规则
最新分类信息我要发布 
最新招聘信息

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放  
Copyright ©2003-2024 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.00666