zoukankan      html  css  js  c++  java
  • property中的read,write是什么意思?

    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;

  • 相关阅读:
    小程序自定义日历组件
    uni-app 区分环境
    uniapp开发微信小程序获取用户手机号
    flex布局中子元素宽度失效的问题
    使用 VSCode 创建 SpringBoot RESTful 增删改查接口项目并发布
    [译] 如何使用 WebGL 技术进行风力地图可视化
    Cesium Primitive API 实践:绘制一个三角形
    ArcPy 输出路径神坑:不能有短横线
    【问题记录】本地没有更新远程仓库的jar包
    k8s 的pod按照时间排序
  • 原文地址:https://www.cnblogs.com/Little-Star/p/7485433.html
Copyright © 2011-2022 走看看