zoukankan      html  css  js  c++  java
  • Delphi面向对象的可见性表示符

      Delphi能通过在声明域和方法的时候用protected、private、public、published和automated指示符来对对象提供进一步的控制。使用这些关键字的语法如下

    TSomeObject = class
    private
        APrivateVariable: Integer;
        AnotherPrivateVariable: Boolean;
    protected
        procedure AProtectedProcedure;
        function ProtectMe: Byte;
    public
        constructor APublicContructor;
        destructor APublicKiller;
    published
        property AProperty read AprivateVariable write APrivateVariable;
    end;
    

      在每一个指示符下呢个声明人一多个方法和域。书写是要注意缩进格式。下面是这些指示符的含义:

    1)private

      对象中的这部分只能被相同单元的代码访问。用这个指示符对用户隐藏实现的细节,并组织用户直接修改对象中的敏感部分

    2)protected

      对象中的这部分成员只能被它的派生类访问,这样不仅能使对象向用户隐藏实现的细节,并未对象的派生类提供了最大的灵活性

    3)public

      这部分的域和方法能在程序额任何地方访问,对象的构造器和析构方法通常应该是public

    4)published

      对象的这部分将产生运行期类型信息(RTTI),并使程序的其他部分能访问这部分。Object Inspector用RTTI来产生属性的列表

    5)automated

      这个指示符其实已经不用了,保留这个指示符的目的是为了与Delphi 2.0的代码兼容

      下面的代码是以前介绍过得TMyObject对象,其中通过增加指示符提高了对象的完整性:

    TMyObject = class
    private
        SomeValue: Integer;
        procedure SetSomeValue(AValue: Integer);
    published
        property Value: Integer read SomeValue write SetSomeValue;
    end;
    
    procedure TMyObject.SetSomeValue(AValue: Integer);
    begin
        if SomeValue<>AValue then
            SomeValue:= AValue;
    end;    
    

      

      现在,对象的用户不能直接修改 S o m e Va l u e的值了,要修改对象的数据就必须通过 Va l u e属性来实现。

  • 相关阅读:
    Convert MSTest to Nunit
    ASP.NET MVC RenderPartial to String
    RailsCasts批量下载地址之Ruby代码
    合并CSS和JavaScript(转载)
    删除Windows Service
    WCF和IIS宿主的ASP.NET 共享会话
    远程桌面常用快捷键
    ColdFusion 9 Installation on IIS7
    Ruby正则表达式实践非贪婪量词
    发布时和调试时使用不同的JS
  • 原文地址:https://www.cnblogs.com/xumenger/p/4434353.html
Copyright © 2011-2022 走看看