简体   繁体   English

Java:访问者的命名约定

[英]Java : naming convention for accessors

I'm looking for the official naming convention in Java regarding accessors. 我正在寻找Java中关于访问器的官方命名约定。

I've seen that, for instance, the JPanel class deprecated the size() method in favor of getSize() . 我已经看到,例如, JPanel类弃用了size()方法而转而使用getSize()

But in the ArrayList class, the method is size() . 但是在ArrayList类中,方法是size()

So I'm wondering if accessors should be named getXXX() or xXX() ? 所以我想知道访问者是否应该命名为getXXX()xXX()

It's usually a bad idea to not use the JavaBeans convention (getters and setters). 使用JavaBeans约定 (getter和setter)通常是一个坏主意。
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). 它们通过许多框架的反射来使用,特别是EL ,有时你不能在没有权利获取者的情况下访问你的领域(取决于EL风格)。

So your accessors should always be named getXxx() or isXxx() and setXxx() . 因此,您的访问器应始终命名为getXxx()isXxx()setXxx()

size() in the collection framework is an example of "flaw" which can annoy developers (see link below). 集合框架中的size()是一个“缺陷”的例子,可以惹恼开发人员(见下面的链接)。 The choice made by Josh Bloch and Neal Gafter to make it more readable now makes it difficult to obtain in some contexts (EL). Josh Bloch和Neal Gafter选择使其更具可读性,这使得在某些情况下(EL)难以获得。

But remember the JavaBeans convention isn't the Java naming convention. 但请记住,JavaBeans约定不是Java命名约定。


Resources : 资源:

On the same topic : 在同一主题上:

With query methods, I always look at getXXX as something that is provided versus something that is calculated. 使用查询方法,我总是将getXXX视为提供的东西与计算的东西。 The size() method returns the size of the collection which is a derived value, so it makes sense. size()方法返回集合的大小,这是一个派生值,因此它是有意义的。 If you had getSize() my assumption would be that I could somehow set the size (through a constructor or setter method). 如果你有getSize()我的假设是我可以以某种方式设置大小(通过构造函数或setter方法)。

For anything trying to look like a JavaBean, it should be getXXX or isXXX . 对于任何试图看起来像JavaBean的东西,它应该是getXXXisXXX (I can't remember whether hasXXX is valid for Boolean properties as well... not sure.) (我不记得hasXXX是否对布尔属性有效......不确定。)

It makes sense to treat a JPanel in a bean kind of way - for designers etc - but not an ArrayList . 以bean的方式处理JPanel是有意义的 - 对于设计者等 - 但不是ArrayList

Personally I tend to use the getXXX form just for consistency, but I believe the above is the reasoning involved in ArrayList 's naming. 我个人倾向于使用getXXX表单来保持一致性,但我相信以上是ArrayList命名的原因。

This is only a formative addentum to Colin HERBERT's answer which, in my opinion, is enough: 这只是Colin HERBERT答案的形成性标题,在我看来,这已足够:

  • Accessor method signatures should always look like public Type getProperty() . 访问器方法签名应始终看起来像public Type getProperty() Additionally, accessors should always return a copy of the property's value, not the value itself. 此外,访问者应始终返回属性值的副本 ,而不是值本身。
  • Mutator method signatures should always look like public void setProperty(Type value) Mutator方法签名应该总是看起来像public void setProperty(Type value)

Combining an accessor and a mutator gives you a JavaBean property. 组合访问器mutator可以获得JavaBean属性。 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) . JavaBeans本质上不被认为是不可变的,但是如果你想让它变成不可变的,你应该为mutator方法使用以下签名: public YourJavaBean withProperty(Type value) Note that this should always return a completely new YourJavaBean instance with copied property values. 请注意,这应始终返回具有复制属性值的全新YourJavaBean实例。

It is always better to follow setXXX and getXXX pattern as per JavaBeans specification. 按照JavaBeans规范,最好遵循setXXX和getXXX模式。 The size() method named so, may be as it is just querying the state. 名为so的size()方法可能就像查询状态一样。

In Eclipse the convention is definitely to use the get pattern. 在Eclipse中,约定肯定是使用get模式。 The automisation tools create and handle getters by checking for and writing get and set style accessors. 自动化工具通过检查和编写getset样式访问器来创建和处理getter。

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. 我更喜欢get / is / set -Convention(特别是对于ValueObjects / DataObjects),不仅因为它是JavaBeans规范,还因为以下两点。

  1. The clear prefix of the method seperates property related methods from 'logic' methods. 方法的明确前缀从“逻辑”方法中分离属性相关的方法。
  2. You can use it out of the box with JSP and other frameworks that assume this naming. 您可以使用JSP和其他假定此命名的框架开箱即用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM