zoukankan      html  css  js  c++  java
  • Delphi的类和对象(二)- 类的属性

     
    unit Unit2;
    
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      end;
    
      //TMyClass1 类里面只有两个字段(变量来到类里面称做字段)
      TMyClass1 = class
        FName: string;  {字段命名一般用 F 开头, 应该是取 field 的首字母}
        FAge:  Integer; {另外: 类的字段必须在方法和属性前面}
      end;
      {这个类中的两个字段, 可以随便读写; 在实际运用中, 这种情况是不存在的.}
    
    
      //TMyClass2 类里面包含两个属性(property)、两个方法、两个和 TMyClass1 相同的字段
      TMyClass2 = class
       private
        FName: string;
        FAge:  Integer;
        procedure SetAge(const Value: Integer);
        procedure SetName(const Value: string);
      published
        property Name: string  read FName write SetName;
        property Age:  Integer read FAge  write SetAge stored True default 88;//设置默认值并将属性值存储到窗体文件中(.dfm)
      end;
      //TMyClass3继承自TMyClass2
      TMyClass3 = class(TMyClass2)
      public
        property Age NoDefault;
        {//当在父类中声明了一个带有默认值的属性后,要对其进行覆盖时,
        如果不指定新的默认值,应使用NoDefault关键字}
      end;
      {
        但这里的字段: FName、FAge 和方法: SetAge、SetName 是不能随便访问的,
        因为, 它们在 strict private 区内, 被封装了, 封装后只能在类内部使用.
    
        属性里面有三个要素:
        1、指定数据类型: 譬如 Age 属性是 Integer 类型;
        2、如何读取: 譬如读取 Age 属性时, 实际上读取的是 FAge 字段;
        3、如何写入: 譬如希尔 Age 属性时, 实际上是通过 SetAge 方法.
    
        属性不过是一个桥.
    
        通过属性存取字段 和 直接存取字段有什么区别?
        通过属性可以给存取一定的限制,
        譬如: 一个人的 age 不可能超过 200 岁, 也不会是负数; 一个人的名字也不应该是空值.
    
        看 implementation 区 TMyClass2 类的两个方法的实现, 就增加了这种限制.
       }
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    
    { TMyClass2 }
    
    procedure TMyClass2.SetAge(const Value: Integer);
    begin
      if (Value>=0) and (Value<200) then
        FAge := Value;
    end;
    
    procedure TMyClass2.SetName(const Value: string);
    begin
      if Value<>'' then
        FName := Value;
    end;
    
    
    //测试:
    procedure TForm1.Button1Click(Sender: TObject);
    var
      class1: TMyClass1;
      class2: TMyClass2;
    begin
      class1 := TMyClass1.Create;
      class2 := TMyClass2.Create;
    
      class1.FAge := 1000; {TMyClass1 中的 FAge 字段可以接受一个离奇的年龄}
      class2.Age  := 99;   {通过 TMyClass2 中的 Age 属性, 只能赋一个合理的值}
      //class2.FAge := 99; {TMyClass2 中的 FAge 字段被封装了, 在这里无法使用}
    
      class1.Free;
      class2.Free;
    end;
    
    end.

    属性的存储: stored

    property ButtonSize: Integer read FButtonSize write SetButtonSize default 0;
        property Color: TColor read FColor write SetColor default clBtnHighlight;
        property Increment: TScrollBarInc read FIncrement write FIncrement stored IsIncrementStored default 8;
        property Margin: Word read FMargin write FMargin default 0;
        property ParentColor: Boolean read FParentColor write SetParentColor default True;
        property Position: Integer read FPosition write SetPosition default 0;
        property Range: Integer read FRange write SetRange stored IsRangeStored default 0;
        property Smooth: Boolean read FSmooth write FSmooth default False;
        property Size: Integer read FSize write SetSize default 0;
        property Style: TScrollBarStyle read FStyle write SetStyle default ssRegular;
        property ThumbSize: Integer read FThumbSize write SetThumbSize default 0;
        property Tracking: Boolean read FTracking write FTracking default False;
        property Visible: Boolean read FVisible write SetVisible default True;

    这是类TControlScrollBar中的属性定义,其中的Range属性中存在stored IsRangeStored定义。

     先说一下这个命令的语法:stored 后面加一个Boolean类型的变量声明,例如stored False 或 stored true

    那么这个stored 是什么意思呢?
    总所周知,Delphi中的控件属性实际上是记录在.dfm文件中,编译时编译进入Exe的。这个stored就是确定是否将该属性记录在.dfm文件中,如果不记录是什么意思,其实也就是这个属性是失效的。

    说白了就是可以通过条件控制这个属性是否可以使用。为什么要这样做呢?其实原因是这样的:

    可以使用这个stored命令来设定互斥属性,什么是互斥属性?也就是一个属性使用的同时就禁止另一个属性的使用。上面的例子中stored IsRangeStored 其中IsRangeStored 是一个函数
    function TControlScrollBar.IsRangeStored: Boolean;
    begin
      Result := not FControl.AutoScroll;
    end;
    也就是说当AutoScroll为True时Stored为False,也就是不记录,反之亦然,说白了就是有你没我,有我没你。使用这个属性可以防止属性之间的相互干扰。

    Default这个命令就很简单,用于设定这个属性的初始值(注意这个Default只能用于设定ordinal, pointer or small set type)。不过这个初始值可以记录在.dfm中的,与在类的Create中赋值是不同的,嘿嘿,不过效果没什么区别。

    转载:https://www.cnblogs.com/del/archive/2008/01/11/1034523.html

       https://www.cnblogs.com/h2zZhou/p/5909274.html

  • 相关阅读:
    关于LEA指令(单周期就可以做简单的算术计算)
    说说字符集和编码(非常形象,非常有意思)
    类依赖项的不透明性和透明性
    构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(6)-Unity 2.x依赖注入by运行时注入[附源码]
    大端和小端
    Google C++测试框架系列:入门
    随机森林中的数学基础之大数定律
    K-means Algorithm
    JavaScript插件——按钮
    分布式系统设计(8)
  • 原文地址:https://www.cnblogs.com/fansizhe/p/12722565.html
Copyright © 2011-2022 走看看