read读取此属性的值
write设置此属性的值
delfault默认值
dynamic表明该方法为动态的方法

 

关于property.
在Delphi当中,往往将一个类的变量定义在private或者protected里面,这样外部是访问不到这些变量的。当有需要将某个或者某些变量暴露出来给外界访问,就在pulic区或者published区定义一个property。property后面跟着的read表示外界引用这个property的时候,从什么地方返回值,write表示外界给这个property赋值的时候,把这个值放到什么地方去,default对于写控件才有用,表示属性栏里面显示的该属性的缺省值。例如:

TMyClass = Class
private
  FField1: integer;
  FField2: string;
  FField3: boolean;

  function GetField3: boolean;
  procedure SetField3(AField: boolean);
public
  property Field1: integer read FField1 write FField1;
published
  property Field2: string read FField2;
  property Field3: boolean read GetField3 write SetField3;
end;

implements

function TMyClass.GetField3: boolean;
begin
  //注意这里用FField3而不是Field3.
  result := FField3;
end;

procedure TMyClass.SetField3(AField: boolean);
begin
  //注意这里用FField3而不是用Field3,因为Field3是专门供外部使用的。
  FField3 := AField;
end;


//////////////////////////
//现在你可以这样调用了:
var
  myClass: TMyClass;
  i: integer;
  s: string;
  b: boolean;
begin
  myClass := TMyClass.Create;
  try
    myClass.Field1 := 1;
    i := myClass.Field1;

    s := myClass.Field2;
    myClass.Field2 := '这句出错,因为Field2是只读的,没有Write';

    myClass.Field3 := true; //自动调用TMyClass.SetField3(True)
    b := myClass.Field3;    //自动调用TMyClass.GetField3返回结果给b
  finally
    myClass.Free;
  end;
end;

posted on 2017-09-06 16:18  敲代码的小女孩  阅读(1209)  评论(0编辑  收藏  举报