商务类中属性的使用与 getter/setter 的比较
在处理业务类(例如典型的 Customer 和 Employee 类)时,仅使用 getter 和 setter 还是使用属性更好?
我正在将java书籍中的一些OO示例翻译为Delphi(用于自学),在这些示例中总是有GetName()和SetName(),不使用属性。
现在,我可以看到,如果我创建一个具有已发布属性的组件,我就有充分的理由使用属性,但在普通类中,哪种方法更好?使用 getter 和 setter(强调我们正在读取/写入属性的事实)或使用属性(乍一看可能与无参数方法混淆)的代码是否更具可读性?
When dealing with buisness classes, like the typical Customer and Employee classes, is it better to use getters and setters only or to use properties?
I am translating to Delphi (for self learning) some OO examples from java books, in those examples there is always GetName() and SetName(), properties are not used.
Now, I can see that if I create a component with published properties I have a very good reason for using properties, but in normal classes, which approach is better? Is the code More Readable with getters and setters (that emphasize the fact we are reading/writing a property) or with properties (that at first sight can be confused with parameterless methods)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
哇。属性的含义远不止“它们只是 getter 和 setter 方法的包装”。
属性是一种优雅但功能强大的方法,可以提供对类字段的受控访问。
访问字段
正如已经说过的,您可以直接访问类字段。这确实非常好,并且使代码更加清晰。这也是实现类的可行第一个版本的非常好的方法。
稍后,您可以重新设计类以使用方法验证和操作字段访问。公共接口仍然相同。
控制访问
属性使您可以轻松概览只读/只写字段。例如,只读/不可变类:
多态性
默认属性
在您的类中,您可以定义默认类字段,这意味着您可以直接访问该字段而无需指定属性名称。
Index
使用
Index
关键字,Delphi 会将一个常量值作为参数传递给 getter/setter 方法。一些资源
仍然有一些主题需要涵盖(实现接口、存储值、RTTI/设计时属性等),但是这篇文章开始变得有点长......
更多内容可以在这些站点阅读:
Wow. There is sooo much more to properties than "they'r just wrappers for the getter and setter methods".
Properties are an elegant, yet powerful, way to provoide controlled access to the class fields.
Accessing fields
As already stated, you can access the class fields directly. This is very nice indeed and makes the code clearer. It is also a very nice way to implement a workable first version of your class.
Later on, you can redesign your class to validate and manipulate the field access using methods. The public interface will is still the same.
Controlling access
Properties gives you an easy overview of readonly/writeonly fields. E.g. a readonly/immutable class:
Polymorphism
Default properties
In your class, you can define a default class fields which means that you can access the field directly without specifying the property name.
Index
With the
Index
keyword, Delphi will pass a constant value as an argument to the getter/setter methods.Some resources
There are still some topics to cover (implementing interfaces, stored values, RTTI/design time properties etc), but this post started to get a bit long...
More can be read at these sites:
不。 getter 和 setter 仅在 Java 中存在,因为它没有属性。它使得使用属性的代码更加简洁。然后,如果您需要 getter 或 setter,您可以将其构建到属性中,但不必在访问代码中添加一堆函数调用。
No. Getters and setters are only there in Java because it doesn't have properties. It makes for much cleaner code to use properties. And then if you need a getter or a setter, you can build it into the property, but you don't have to litter the accessing code with a bunch of function calls.
属性是Delphi的一大优势。我自己已经做过相当多的 Java 和 Delphi 工作,并且肯定会选择 Properties。实际上,这两种方法最终都会做同样的事情,唯一的区别是属性看起来比 getter/setter 整洁得多。
我建议你去购买房产,并充分利用它们!
Properties are a great advantage of Delphi. I have done quite a bit of Java and Delphi myself and definitely would go for Properties. Practically both approaches will eventually do the same thing, with the only difference that properties look MUCH neater than getters/setters.
I suggest you go for properties, and take full advantage of them!
这确实是一个品味和用途的问题。
对于像 pascal 这样的程序员来说,如果你正在读取或写入一个值,这是非常清楚的,所以我认为如果你不以类似 java 的方式使用 getter 和 setter,即在每一个句子中编写 GetXXX 或 SetXXX,那么代码的可读性会更高你的程序。
对于我来说,我假设对于大多数 pascal 程序员来说,如果您只输入正在读取/写入的属性的名称,则代码更具可读性,并且我们都知道将调用 Getter 或 Setter 方法(如果需要) 。
另外,我认为 delphi 属性模型的一个巨大好处(和优雅)是,您可以直接从字段获取/设置属性值 只需一行代码即可获得大量 Get/Set 方法,其中值是从字段中分配或读取完全是浪费精力/时间。
It really is a matter of taste and use.
For pascal like programmers, it's very clear if you're reading or writing a value, so I think the code is more readable if you do not use getters and setters in the java-like way where you write GetXXX or SetXXX in every sentence of your program.
For me, and I assume for the majority of pascal programmers, the code is more readable if you just put the name of the property you're reading/writing, and we all know a Getter or Setter method will be called (if necessary).
Also, I think it is a huge benefit (and elegance) from the delphi property model that you can get/set the property value directly from a field To have a lot of Get/Set methods with just a line of code where the value is assigned or read from a field is a total waste of effort/time.