在业务类中使用属​​性vs getters / setters [英] usage of property vs getters/setters in business classes

查看:135
本文介绍了在业务类中使用属​​性vs getters / setters的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当处理商业类时,像典型的客户和员工类,最好只使用getters和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?

Delphi(自学习)一些来自java书籍的例子,在那些例子中总是有GetName()和SetName(),属性没有使用。

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.

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

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)?

推荐答案

否。 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.

这篇关于在业务类中使用属​​性vs getters / setters的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆