gpt4 book ai didi

class - 德尔福类: Property vs Get/Set methods

转载 作者:行者123 更新时间:2023-12-03 15:16:25 27 4
gpt4 key购买 nike

所以我对面向对象编程有点陌生。 Delphi 具有属性,旨在成为比 getters/setters 更“优雅”的访问类数据的方式(在此处阅读 usage of property vs getters/setters in business classes )。

什么时候应该直接使用字段,什么时候应该在属性内使用getters/setters?我只在需要操作数据时进行猜测,但我不确定。

编辑:

省略一个只返回字段本身的值而不执行任何操作的 setter 是错误的吗?

  property Field :integer read FField write FField;

最佳答案

为什么是属性?

对于初学者来说,来自 documentation on properties 的快速摘要:

A property, like a field, defines an attribute of an object. But while a field is merely a storage location whose contents can be examined and changed, a property associates specific actions with reading or modifying its data. Properties provide control over access to an object's attributes, and they allow attributes to be computed.

为什么不只有 setter 和 getter?

存储和访问的分离确实可以通过仅使用 getter 和 setter 来实现,而保留属性。这是事实,但您链接到的问题源于语言差异:Delphi 确实具有属性,并且那里的答案已经解释了为什么使用它们。两个最明显的原因是(1)更清晰的代码和(2)分配能力。我认为this answer已经相当广泛地处理它了。

此外,在不使用属性的情况下,总是需要 getter 和 setter,而使用属性则不需要。假设有 setter 实现,但没有 getter:属性可以直接读取字段。

类(class)完成

当您只声明属性的名称及其类型时,Delphi 的类完成默认读取私有(private)字段和设置私有(private)字段的私有(private) setter。请注意,这只是默认配置,您可以再次根据需要进行修改。当您完全指定属性声明时,类完成将遵守并根据声明的需要添加私有(private)字段、getter 和/或 setter。

没有 getter 和 setter 的属性

Is it wrong to omit a setter that does nothing but return the value of the field itself?

当一个属性没有 getter 或 setter 并且它只是读取和写入字段时,您可以得出这样的结论:除了一致之外没有任何区别。但事实并非如此。字段和属性具有不同的名称,因此可能具有不同的含义。意思是你可以给予。请参阅Using properties instead of fields in class methods of the same unit is a bad practice? .

何时使用 getter 或 setter?

... I'm gessing only when the data needs to be manipulated ...

嗯,这部分是正确的。操纵是众多原因之一。考虑 String 类型的 Price 属性及其私有(private)字段 FPrice:

  • 限制:当价格需要等于或大于零时,
  • 委托(delegate):当FPrice是另一个字段的一部分时,或者当它超出此类的责任时,
  • 验证:当价格逗号后面只能有两位小数时,
  • 解释:当价格以千为单位输入,但应以美分存储时,
  • 影响:当价格对另一个字段(例如费率或 margin )产生影响时,
  • 激活:当编辑价格需要立即采取操作(例如更新 GUI)时,
  • 转换:当价格以美元输入但应以日元存储时,
  • 取消:当价格没有意义时,例如以科学计数法输入时。

请注意,Price 属性非常初级。将其 setter 或 getter 留给将来的实现是很有可能的。但想象一下没有 setter 或 getter 就无法实现的更高级属性:

  • 咨询前需要创建的字段:

    function TMyObject.GetBarelyUsed: TRare;
    begin
    if FBarelyUsed = nil then
    FBarelyUsed := TRare.Create(Self);
    Result := FBarelyUsed;
    end;
  • 可以选择一个项目,但该项目本身不知道要做什么。相反,业主这样做了。请注意,在本例中完全没有私有(private)字段:

    procedure TItem.SetSelected(Value: Boolean);
    begin
    if Value <> Selected then
    begin
    if Value then
    Owner.Selection.Add(Self)
    else
    Owner.Selection.Remove(Self);
    end;
    end;
  • 图像控件,专门用于查看您自己的图像格式。 FileName 属性的赋值涉及:检查正确的文件扩展名、检查文件是否存在、将文件名存储在私有(private)字段中、加载文件、调整图片的尺寸,或者撤消之前的赋值:

    procedure TAwDxfImage.SetFileName(const Value: TFileName);
    begin
    if FFileName <> Value then
    if SameText(ExtractFileExt(Value), '.' + SDxf) and
    FileExists(Value) then
    begin
    FFileName := Value;
    FGraphic.LoadFromFile(FFileName);
    FGraphic.SetBounds(Width, Height);
    end
    else
    begin
    FFileName := '';
    FGraphic.Clear;
    end;
    end;

<子> Source: NLDelphi

关于class - 德尔福类: Property vs Get/Set methods,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28884568/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com