zoukankan      html  css  js  c++  java
  • Delphi的类和对象(三)- 使用数组属性和属性的索引

    类中使用数组属性,声明方式如下:

    property 属性名[参数列表]:属性类型 Read 方法名 Write 方法名;

    声明数组属性时要注意:

    (1)参数表与过程或函数参数表非常相似,只是用方括号,参数表中的参数类型可以是任何类型。

    (2)声明数组属性时,访问说明中在Read 或Write 后面的必须是方法名,不能出现数据成员。

    (3)Read 后面的方法参数表的参数类型和顺序必须与属性参数表相同,Write 后得过程方法的参数表中必须列出属性的参数表且与该列表的顺序与属性参数表相同。

        在该过程方法的最后,是一个与属性类型相同的值或常数参数。

    (4)数组属性可以通过它的下标访问,下表是传递给读或写方法的参数。

    (5)在数组属性的生命中,不能进行存储声明。但可以使用Default 指令符,这时该指令符不是对存储的说明,而是指定当前的属性为类中的默认属性。

    示例:

    unit Unit4;
    
    interface
    
    type
      Tmyclass = class
      private
        FNumber: array[0..100] of String; //读写方法私有化(get set)
        function GetNuber(x: Integer): String;
        procedure SetNumber(x: Integer; const Value: String);
      public
        property Numbers[x: Integer]:String read GetNuber write SetNumber;//数组属性 public
      end;
    
    implementation
    
    { Tmyclass }
    
    function Tmyclass.GetNuber(x: Integer): String;
    begin
      Result:= FNumber[x];
    end;
    
    procedure Tmyclass.SetNumber(x: Integer; const Value: String);
    begin
      FNumber[x]:= Value;
    end;
    
    end.

    属性的索引:索引说明用来使多个属性共用一个访问方法来设置属性的值。格式如下:

    unit Unit4;
    
    interface
    
    type
      Tmyclass = class
      private
        FPostion: array[0..2]of Integer;
        function getpostion(const Index: Integer): Integer;
        procedure setpostion(const Index, Value: Integer);
    
      public
        property left: Integer index(0) read getpostion write setpostion;
        property top: Integer index(1) read getpostion write setpostion;
      end;
    {属性声明中读写属性的说明必须是方法,read 后面的方法必须附加一个整型的参数,
      write 后的过程方法必须在参数表的倒数第二个参数位置附加一个整型的参数}
    implementation
    
    { Tmyclass }
    
    function Tmyclass.getpostion(const Index: Integer): Integer;
    begin
      Result:= FPostion[Index];
    end;
    
    procedure Tmyclass.setpostion(const Index, Value: Integer);
    begin
      FPostion[Index]:= Value;
    end;
    
    end.
  • 相关阅读:
    Promise 对象
    [转] LVM分区在线扩容
    [转] 打开 CMD 时自动执行命令
    [转] FFmpeg常用基本命令
    systemd 之 journalctl
    systemd 之 systemctl
    关于用户权限的加强与理解(上)
    [转] 测试环境下将centos6.8升级到centos7的操作记录
    [搞机] 双网卡做数据均衡负载
    [转] 网络基础知识1:集线器,网桥,交换机
  • 原文地址:https://www.cnblogs.com/fansizhe/p/12722960.html
Copyright © 2011-2022 走看看