⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 访问器的威力(属性访问器) (2001年5月26日).txt

📁 delphi 编程技巧
💻 TXT
字号:
访问器的威力(属性访问器) (2001年5月26日) 

本站更新  分类:控件使用   作者:Pieter Valentijn  推荐:   阅读次数:180  
(http://www.codesky.net)  

--------------------------------------------------------------------------------

属性访问器非常强大。
它使你能完成储如初始化对象或生成数据等要依靠对象的其它属性的工作。
你能这样来新建属性:先在接口段中声明它们然后按Ctrl+Shift+''c''。
这样就会新建一个称为Fsomeproperty的私有变量,和一个称为SetSomeProperty的方法。

在本例中我创建了一个对象,它具有控制其它对象的属性。通过此属性的Read操作,我会检查它是否为零,以便在运行时创建它。
你还可以用此属性的Set访问器来检查数值是否超出边界。
type
TMySubObject = Class

end;

TMyObject = Class
private
fMySubObject : TMySubObject 
FASecondNum: Integer;
FAFirstnum: Integer;
function getMySubObject: TMySubObject;
function GetTotaalOfFirstAndSecondNum: integer;
procedure SetFirstnum(const Value: Integer);
public

Property MySubObject : TMySubObject Read getMySubObject 
Property AFirstnum : Integer read FAFirstnum write SetFirstnum;
Property ASecondNum : Integer read FASecondNum write FAFirstnum;
Property TotaalOfFirstAndSecondNum : integer read 
GetTotaalOfFirstAndSecondNum 
end;


implementation

{$R *.DFM}

{ TMyObject }

function TMyObject.getMySubObject: TMySubObject;
begin
//我在此检查它是否已赋值。
If not Assigned(fMySubObject) Then fMySubObject := TMySubObject.Create 
Result := fMySubObject 
end;


function TMyObject.GetTotaalOfFirstAndSecondNum: integer;
begin
Result := FAFirstnum + FASecondNum 
end;

procedure TMyObject.SetFirstnum(const Value: Integer);
begin
//在此通过set可以检查边界。
If (Value > 0) and (Value < 1000) then
FAFirstnum := Value
Else Raise Exception.Create(''Number out of bounds'');
end; 
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -