[英]difference between a getter method and a method which returns the state of an instance variable?
I am reading through the Guava library, and I keep running into methods that look something like this: 我正在阅读Guava库,我一直在尝试看起来像这样的方法:
@Override public int size() {
return size;
}
What is the difference (strategically, conventionally, etc) between the above and the following? 上面和下面之间有什么区别(战略上,传统上等)?
@Override public int getSize() {
return size;
}
Or is there no difference? 或者没有区别? Is it just shorthand? 这只是简写吗?
One form isn't using JavaBeans conventions . 一种形式不使用JavaBeans约定 。 Functionally speaking, systems that expect you to follow those conventions will not work or be very cumbersome to set up if you use non-conventional getters/setters for your beans, but if you're not , then there's no real difference. 从功能上讲,如果你使用非传统的getter / setter来为你的bean设置,那些希望你遵循这些约定的系统将无法工作或设置非常麻烦,但如果你不是 ,那么就没有真正的区别。
Since Guava has a lot of collections, and the Collection
interface actually defines a size()
method , my gut tells me that Guava is more inclined to follow the Collection
interface than JavaBeans conventions. 由于Guava有很多集合,而Collection
接口实际上定义了一个size()
方法 ,我的直觉告诉我Guava更倾向于遵循Collection
接口而不是JavaBeans约定。
getAttribute
says more about what this method does ( size
can imply that we need to do some additional calculations, getters in most cases simply returns value). 在可读性方面,它们也很相似,但getAttribute
更多地说明了这个方法的作用 ( size
可能意味着我们需要做一些额外的计算,在大多数情况下,getter只是返回值)。 Anyway about your example. 无论如何关于你的例子。 I suspect that since List
s are not supposed to be treated as JavaBean
s there is no requirement to return size
with getSize()
so simple size()
is enough. 我怀疑由于List
不应被视为JavaBean
,因此不需要使用getSize()
返回size
,因此简单的size()
就足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.