在业务类中使用属​​性vs getter/setter

LaB*_*cca 15 delphi coding-style properties getter-setter

在处理商业类时,比如典型的Customer和Employee类,最好只使用getter和setter还是使用属性?

我正在翻译Delphi(用于自学)一些来自java书籍的OO示例,在这些示例中总是有GetName()和SetName(),不使用属性.

现在,我可以看到,如果我创建一个具有已发布属性的组件,我有一个很好的理由使用属性,但在正常的类中,哪种方法更好?使用getter和setter(强调我们正在读/写属性的事实)或属性(乍一看可能与无参数方法混淆)的代码是否更易读?

Jør*_*eit 36

哇.除了"他们只是getter和setter方法的包装器"之外,属性还有很多东西.

属性是一种优雅而强大的方式,可以提供对类字段的受控访问.

访问字段

如前所述,您可以直接访问类字段.这确实非常好,使代码更清晰.这也是实现类的可行第一版的一种非常好的方法.

TMyClass = class
private
  FValue: String;
public
  property Value: String read FValue write FValue;
end; 
Run Code Online (Sandbox Code Playgroud)

稍后,您可以重新设计类以使用方法验证和操作字段访问.公共接口仍然是相同的.

TMyClass = class
private
  FValue: String;
  procedure SetValue(AValue: String);
public
  property Value: String read FValue write SetValue;
end; 

procedure TMyClass.SetValue(AValue: String);
begin
  if AValue = '' 
  then FValue := 'No value!'
  else FValue := AValue;
end;
Run Code Online (Sandbox Code Playgroud)

控制访问

属性为您提供了readonly/writeonly字段的简单概述.例如,readonly/immutable类:

TClient = class
private
  FName: String;
  FSite: String;
  FMail: String;
public
  constructor Create(AName, ASite, AMail: String);
  property Name: String read FName;
  property Site: String read FSite;
  property Mail: String read FMail;
end; 
Run Code Online (Sandbox Code Playgroud)

多态性

TClient = class
private
  FName: String;
protected
  function GetName: String; virtual; abstract;
public
  property Name: String read GetName write FName;
end; 

TImportantClient = class(TClient)
protected
  function GetName: String; override;
end; 

TArgumentativeClient = class(TClient)
protected
  function GetName: String; override; 
end; 

function TImportantClient.GetName: String; 
begin
  Result := '+++ ' + FName;
end; 

function TArgumentativeClient.GetName: String; 
begin
  Result := ':-( ' + FName;
end; 

{----- ----- ----- ----- -----}
var
  ClientA,
  ClientB: TClient;
begin
  ClientA := TImportantClient.Create;
  ClientB := TArgumentativeClient.Create;

  ClientA.Name := 'Mr. Nice';
  ClientB.Name := 'Mr. Dumbhead';

  ShowMessage(ClientA.Name);
  ShowMessage(ClientB.Name);
end;
{----- ----- ----- ----- -----}
Run Code Online (Sandbox Code Playgroud)

默认属性

在您的类中,您可以定义默认类字段,这意味着您可以直接访问该字段而无需指定属性名称.

A := MyStringList[i]:
MyStringList[i] := B;

{ instead of }

A := MyStringList.Strings[i];
MyStringList.Strings[i] := B;

{ or }

A := MyStringList.GetString(i);
MyStringList.SetString(i, B);
Run Code Online (Sandbox Code Playgroud)

指数

使用Index关键字,Delphi会将一个常量值作为参数传递给getter/setter方法.

TMyRect = class
private
  FValues: Array[0..3] of Integer;
  function GetProperty(Index: Integer): Integer;
public
  property Top    : Integer  Index 0  read GetProperty;
  property Left   : Integer  Index 1  read GetProperty;
  property Width  : Integer  Index 2  read GetProperty;
  property Height : Integer  Index 3  read GetProperty;
end;


function TMyRect.GetProperty(Index: Integer): Integer;
begin
  Result := FValues[Index];
end; 
Run Code Online (Sandbox Code Playgroud)

一些资源

仍然有一些主题要涵盖(实现接口,存储值,RTTI /设计时间属性等),但这篇文章开始变得有点长......

可以在这些网站上阅读更多内容:

  • *当然还有更多的属性*。 (2认同)

Mas*_*ler 13

不会.Getters和setter只在Java中存在,因为它没有属性.它使得使用属性的代码更加清晰.然后,如果你需要一个getter或setter,你可以将它构建到属性中,但是你不必通过一堆函数调用来访问访问代码.


Ed.*_*d.C 10

属性是Delphi的一大优势.我自己做了很多Java和Delphi,肯定会选择Properties.实际上两种方法最终都会做同样的事情,唯一的区别是属性看起来比吸气剂/设定者更整洁.

我建议你去寻找房产,并充分利用它们!