Java:访问器的命名约定
我正在寻找 Java 中有关访问器的官方命名约定。
例如,我发现 JPanel
类弃用了 size()
方法,转而使用 getSize()
。
但在ArrayList
类中,该方法是size()
。
所以我想知道访问器是否应该命名为 getXXX()
或 xXX()
?
I'm looking for the official naming convention in Java regarding accessors.
I've seen that, for instance, the JPanel
class deprecated the size()
method in favor of getSize()
.
But in the ArrayList
class, the method is size()
.
So I'm wondering if accessors should be named getXXX()
or xXX()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不使用JavaBeans 约定(getter 和 setter)通常是个坏主意。
许多框架通过反射来使用它们,特别是EL,有时如果没有权限获取器就无法访问您的字段(取决于 EL 风格)。
因此,您的访问器应始终命名为
getXxx()
或isXxx()
和setXxx()
。集合框架中的
size()
是一个“缺陷”的示例,它可能会惹恼开发人员(请参阅下面的链接)。 Josh Bloch 和 Neal Gafter 做出的选择使其更具可读性,现在使其难以在某些上下文中获取(EL)。但请记住 JavaBeans 约定不是 Java 命名约定。
资源:
同一主题:
It's usually a bad idea to not use the JavaBeans convention (getters and setters).
They're used through reflection by many frameworks, in particular with EL where sometimes you can't access your fields without the rights getters (depending on the EL flavour).
So your accessors should always be named
getXxx()
orisXxx()
andsetXxx()
.size()
in the collection framework is an example of "flaw" which can annoy developers (see link below). The choice made by Josh Bloch and Neal Gafter to make it more readable now makes it difficult to obtain in some contexts (EL).But remember the JavaBeans convention isn't the Java naming convention.
Resources :
On the same topic :
对于查询方法,我总是将
getXXX
视为提供的内容与计算的内容。size()
方法返回集合的大小,这是一个派生值,因此它是有意义的。如果你有 getSize() 我的假设是我可以以某种方式设置大小(通过构造函数或 setter 方法)。With query methods, I always look at
getXXX
as something that is provided versus something that is calculated. Thesize()
method returns the size of the collection which is a derived value, so it makes sense. If you hadgetSize()
my assumption would be that I could somehow set the size (through a constructor or setter method).对于任何试图看起来像 JavaBean 的东西,它应该是
getXXX
或isXXX
。 (我不记得 hasXXX 对于布尔属性是否也有效......不确定。)以 bean 的方式处理
JPanel
是有意义的 - 对于设计师等 - 但不是数组列表。就个人而言,我倾向于使用 getXXX 形式只是为了保持一致性,但我相信以上是
ArrayList
命名所涉及的推理。For anything trying to look like a JavaBean, it should be
getXXX
orisXXX
. (I can't remember whether hasXXX is valid for Boolean properties as well... not sure.)It makes sense to treat a
JPanel
in a bean kind of way - for designers etc - but not anArrayList
.Personally I tend to use the getXXX form just for consistency, but I believe the above is the reasoning involved in
ArrayList
's naming.这只是 Colin HERBERT 答案的一个补充,在我看来,这已经足够了:
public Type getProperty()
。此外,访问器应始终返回属性值的副本,而不是值本身。public void setProperty(Type value)
组合 访问器 和 mutator 可以为您提供JavaBean 属性。 JavaBean 本质上不被认为是不可变的,但如果您想使其不可变,您应该对 mutator 方法使用以下签名:
public YourJavaBean withProperty(Type value)
。请注意,这应该始终返回一个带有复制属性值的全新YourJavaBean
实例。This is only a formative addentum to Colin HERBERT's answer which, in my opinion, is enough:
public Type getProperty()
. Additionally, accessors should always return a copy of the property's value, not the value itself.public void setProperty(Type value)
Combining an accessor and a mutator gives you a JavaBean property. JavaBeans are not considered to be immutable by nature, but if you want to make it immutable, you should use the following signature for the mutator method:
public YourJavaBean withProperty(Type value)
. Note that this should always return a completely newYourJavaBean
instance with copied property values.在 Eclipse 中,约定肯定是使用
get
模式。自动化工具通过检查和编写get
和set
样式访问器来创建和处理 getter。In Eclipse the convention is definitely to use the
get
pattern. The automisation tools create and handle getters by checking for and writingget
andset
style accessors.我更喜欢
get
/is
/set
约定(特别是对于 ValueObjects/DataObjects),不仅因为它是 JavaBeans 规范而是因为以下两点。I prefer the
get
/is
/set
-Convention (especially for ValueObjects/DataObjects) not only because it's the JavaBeans specification but because of the following two points.根据 JavaBeans 规范,遵循 setXXX 和 getXXX 模式总是更好。 size() 方法如此命名,可能是因为它只是查询状态。
It is always better to follow setXXX and getXXX pattern as per JavaBeans specification. The size() method named so, may be as it is just querying the state.