zoukankan      html  css  js  c++  java
  • delphi property read writer 如何使用

    type
    TMyClass = class(TObject)
    private
    FMyName: string;
    FMyAge: Integer;
    procedure SetAge(age: Integer);
    function GetAge(): Integer;
    published
    property MyName: string read FMyName write FMyName;
    property MyAge: Integer read GetAge write SetAge;
    end;

    procedure TMyClass.SetAge(age: Integer);
    begin
    if (age < 0) or (age > 200) then
    ShowMessage('当前设置的年龄数值: ' + IntToStr(age) + '不是有效的年龄数值')
    else FMyAge := age;
    end;

    function TmyClass.GetAge: Integer;
    begin
    Result := FMyAge;
    end;

    // 测试
    procedure TForm1.Button1Click(Sender: TObject);
    var
    ta: TMyClass;
    begin
    ta := TMyClass.Create;
    ShowMessage('myname:' + ta.MyName + ', myage:' + IntToStr(ta.MyAge));
    ta.MyName := 'Tom';
    ta.MyAge := -10;
    ShowMessage('myname:' + ta.MyName + ', myage:' + IntToStr(ta.MyAge));
    ta.MyAge := 22;
    ShowMessage('myname:' + ta.MyName + ', myage:' + IntToStr(ta.MyAge));
    ta.free;
    end;
  • 相关阅读:
    TinyOS在ubuntu 14.04下安装教程
    C++ STL标准入门
    C++ 模板
    多态
    C++继承
    C++类型转换 -- 由其他类型转换到自定义类型
    运算符重载
    友元
    typedef用法
    c++细节--section1
  • 原文地址:https://www.cnblogs.com/jijm123/p/7583235.html
Copyright © 2011-2022 走看看