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;

posted on   癫狂编程  阅读(974)  评论(5编辑  收藏  举报

编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
好的代码像粥一样,都是用时间熬出来的
点击右上角即可分享
微信分享提示