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

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

问题描述

在处理业务类时,如典型的 Customer 和 Employee 类,是只使用 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?

我正在将 Java 书籍中的一些 OO 示例翻译到 Delphi(用于自学),在这些示例中,始终有 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.

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

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 getter/setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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