简体   繁体   English

getter方法和返回实例变量状态的方法之间的区别?

[英]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约定。

  • In terms of performance there is no difference. 在性能方面没有区别。
  • In terms of readability they are also similar but 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只是返回值)。
  • But I suspect that main reason that people tend to name their methods as get/set... is to let their class become proper JavaBean which will be properly handled by tools which are using JavaBeans like Expression Language . 但我怀疑人们倾向于将他们的方法命名为get / set的主要原因是让他们的类成为正确的JavaBean,这些JavaBean将由使用JavaBeans(如Expression Language)的工具正确处理。

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.

相关问题 在getInstance()方法或实例变量定义中初始化单例是否存在功能差异 - Is there a functional difference between initializing singleton in a getInstance() method, or in the instance variable definition Java:getter方法与公共实例变量:性能和内存 - Java: getter method vs. public instance variable: performance and memory 两种方法对实例方法的引用之间的区别 - Difference between the two method references to instance methods 使用getter方法在构造函数中初始化的变量 - Variable to be initialized in a constructor with getter method 将@Autowired放到变量和方法之间有什么区别? - What is the difference between putting @Autowired to a variable and a method? 访问getter时方法返回null - Method returns null when accessing getter Getter方法返回null。 Java MVC - Getter method returns null. Java MVC 如何正确生成使用Reflection API返回实例的方法? - How to proper generify a method which returns an instance using Reflection API? JAVA:使用className和实例的Object进行静态方法调用之间的区别 - JAVA: difference between Static method invocation with className and with Object of instance Java 8 中的实例方法引用类型有什么区别? - What's the difference between instance method reference types in Java 8?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM