在Delphi的属性 property中使用数组

来源:互联网 发布:xbox360手柄连接mac 编辑:程序博客网 时间:2024/06/01 09:32
unit Unit1;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs, StdCtrls;type    TGCNodeRec = record        QVehicleArrayID:Integer;        GCCount:Integer;        GCDirection:string;        QCDirection:string;end;type  TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  private    { Private declarations }  public    { Public declarations }  end;  type Ts = array[0..19] of TGCNodeRec; type  TCompass = class  private    FHeading: Ts;    function GetHeading(index:Integer): TGCNodeRec;    procedure SetHeading(index:Integer;Value: TGCNodeRec);  public   //注以下定义属性,Published中不允许使用,在Public中运行正常 ;   Published中不允许使用数组    property PHeading[index:Integer]: TGCNodeRec read GetHeading  write SetHeading; default;  end;var  Form1: TForm1;implementation{$R *.dfm}function TCompass.GetHeading(index:Integer): TGCNodeRec;begin    Result := fheading[index];end;procedure TCompass.SetHeading(index:Integer;Value: TGCNodeRec);begin    FHeading[index] := Value;end;procedure TForm1.Button1Click(Sender: TObject);var MyClass:TCompass;    GcNode : TGCNodeRec;begin    GcNode.GCDirection := '测试';    MyClass := TCompass.create;    MyClass.PHeading[0] := GcNode; //给属性赋值, 自动调用  SetHeading函数为数组0号元素赋值    //以下2种方式调用都可以 返回的结果是一样的    ShowMessage(MyClass.FHeading[0].GCDirection);    ShowMessage(MyClass.PHeading[0].GCDirection );end;end.

 

 

  公布成员(published)和公共成员(public)具有相同的可见度,但published中声明的会显示在属性栏,public   不会!

    published和public的限制访问属性是一样的 ;不过,published一般用于组件编程中,而不常用于应用程序中。

转自OBJECT   PASCAL

     "公布成员(published)和公共成员(public)具有相同的可见度。不同的是,运行时信息RTTI(runtime   type   information)为公布成员产生。RTTI允许应用程序动态地查询对象的域和属性、定位对象的方法。RTTI用于在下列情形下访问属性的值:保存和加载文件时,在对象检查器(Object   Inspector)中显示属性时,将指定的方法(即事件处理程序)与指定的属性(即事件)关联时。

    公布属性只限于某些数据类型。序数、串、类、接口和方法指针等类型可以被公布;由此可见,提供的基类型其上下界范围的序数值在0到31之间的集合类型也可以被公布,即集合必需适合于字节、字或双字(Byte,Word或Double   Word);除Real48之外,任何实数类型都可以被公布。数组类型的属性(不同于下面述及的数组属性array   properties)不能被公布。"


 

原创粉丝点击