Object Pascal 中的属性与事件详解
1. 属性代码补全
在为类添加属性时,IDE 编辑器提供了便捷的代码补全功能。当在类中编写属性声明的初始部分时,例如:
type
TMyClass = class
public
property Month: Integer;
end;
将光标置于该属性声明上,按下 Ctrl + Shift + C 组合键,会为类添加一个新字段和一个新的 setter 方法,并在属性定义中进行正确映射,同时完整实现 setter 方法。上述代码会变为:
type
TMyClass = class
private
FMonth: Integer;
procedure SetMonth(const Value: Integer);
public
property Month: Integer read FMonth write SetMonth;
end;
{ TMyClass }
procedure TMyClass.SetMonth(const Value: Integer);
begin
FMonth := Value;
end;
若还需要 getter 方法,将属性定义中的 read 部分替换为 GetMonth ,如:
超级会员免费看
订阅专栏 解锁全文
6

被折叠的 条评论
为什么被折叠?



