设置对象的值
假设我有一个名为 House
的类,其中包含两个字段,
name
address
每个字段都有一个 getter 和一个 setter。
现在我想要 House
类中的另一个名为 setValues
的方法。此方法应该使用来自不同类型的传递对象的属性来设置字段。
创建此方法有两种方法。第一种方式:
private void setHouse(HouseTransfer transer){
name = transfer.getName();
address = transfer.getAddress();
}
或者第二种选择:
private void setHouse(HouseTransfer transer){
setName(transfer.getName());
setAddress(transfer.getAddress());
}
哪一种更“最佳实践”?
Let's say I've got a class called House
with the two fields
name
address
Each of these fields has got a getter and a setter.
Now I want another method in the House
class called setValues
. This method should set the fields with properties from a passed object of a different type.
There would be two ways on how to create this method. First way:
private void setHouse(HouseTransfer transer){
name = transfer.getName();
address = transfer.getAddress();
}
Or the second option:
private void setHouse(HouseTransfer transer){
setName(transfer.getName());
setAddress(transfer.getAddress());
}
Which one is more "best practice"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在一定程度的粒度上,软件设计比非黑即白的绝对更主观。我不相信这里有绝对“最佳实践”。
话虽这么说,我个人会使用第二种形式。使用 setter 方法的基本思想是,在某些时候您可能需要一些特殊的逻辑来设置该值(例如格式化输入、卫生、验证等)。因此,最有意义的是始终依赖于一个中心位置的任何此类逻辑,而不是分散在设置此变量的代码中的任何位置。
如果您有一个非常简单的示例,其中设置器只是设置值并且绝对知道不会添加其他逻辑,那么为了简单起见,您当然可以使用第一种形式。第二种形式对性能没有真正的影响,所以我个人只会使用它。
At a certain level of granularity, software design is more subjective matter than one of black-and-white absolutes. I do not believe there is an absolute "best practice" here.
That being said, I personally would use the second form. The basic idea of having a setter method is that at some point you might need some some special logic around setting that value (e.g. formatting input, sanitation, validation, etc). So it makes the most sense to always rely on any such logic being in one central place, rather than scattered throughout you code anywhere this variable is set.
If you have a truly trivial example, where the setter is simply setting the value and know absolutely that no other logic will ever be added, then you could certainly use the first form for simplicity. Put there's not real performance hit to the second form, so I personally would just use that.
我将在 setHouse 方法中使用单独的 getter/setter(这是你的第二个选择)。
事实上,您拥有设置器,这表明该操作涉及某种封装。不要重新编写代码来强制封装,而是重新使用已有的代码。
I would use the individual getters/setters inside of the
setHouse
method (which is your second option).The fact that you have setters indicates that there is some kind of encapsulation involved around that operation. Rather than re-write the code to enforce that encapsulation, re-use what you already have.
乔恩对该问题的回答(取自另一个关于使用getter/setter(与此不重复)
您并不总是需要 getter/setter,但如果您有一些,通常有一个很好的理由来实现它们,在这种情况下:使用它们。
Jon's answer to that question (Taken from another question about using getters/setters which is not a duplicate to this one)
You don't always need getters/setters, but if you have some, there's usually a good reason why you've implemented them and in that case: use them.
也许如果您在两个不同的地方获取和设置,您可能会考虑将 getter 和 setter 分解到一个公共接口。这可以让以后的定制变得更容易,对吗?
Perhaps if you are getting and setting in two different places you might consider factoring out your getter and setter to a common interface. This can make later customisations easier, right?