Java:访问器的命名约定

发布于 2024-09-19 12:54:23 字数 254 浏览 2 评论 0原文

我正在寻找 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

开始看清了 2024-09-26 12:54:23

使用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() or isXxx() and setXxx().

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 :

書生途 2024-09-26 12:54:23

对于查询方法,我总是将 getXXX 视为提供的内容与计算的内容。 size() 方法返回集合的大小,这是一个派生值,因此它是有意义的。如果你有 getSize() 我的假设是我可以以某种方式设置大小(通过构造函数或 setter 方法)。

With query methods, I always look at getXXX as something that is provided versus something that is calculated. The size() method returns the size of the collection which is a derived value, so it makes sense. If you had getSize() my assumption would be that I could somehow set the size (through a constructor or setter method).

最美的太阳 2024-09-26 12:54:23

对于任何试图看起来像 JavaBean 的东西,它应该是 getXXXisXXX。 (我不记得 hasXXX 对于布尔属性是否也有效......不确定。)

以 bean 的方式处理 JPanel 是有意义的 - 对于设计师等 - 但不是数组列表。

就个人而言,我倾向于使用 getXXX 形式只是为了保持一致性,但我相信以上是 ArrayList 命名所涉及的推理。

For anything trying to look like a JavaBean, it should be getXXX or isXXX. (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 an 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.

夜吻♂芭芘 2024-09-26 12:54:23

这只是 Colin HERBERT 答案的一个补充,在我看来,这已经足够了:

  • 访问器方法签名应该始终类似于 public Type getProperty()。此外,访问器应始终返回属性值的副本,而不是值本身。
  • Mutator 方法签名应始终类似于 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:

  • Accessor method signatures should always look like 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)

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 new YourJavaBean instance with copied property values.

若沐 2024-09-26 12:54:23

在 Eclipse 中,约定肯定是使用 get 模式。自动化工具通过检查和编写 getset 样式访问器来创建和处理 getter。

In Eclipse the convention is definitely to use the getpattern. The automisation tools create and handle getters by checking for and writing get and set style accessors.

智商已欠费 2024-09-26 12:54:23

我更喜欢 get/is/set 约定(特别是对于 ValueObjects/DataObjects),不仅因为它是 JavaBeans 规范而是因为以下两点。

  1. 方法的明确前缀将属性相关方法与“逻辑”方法分开。
  2. 您可以将它与采用此命名的 JSP 和其他框架一起使用。

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.

  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.
半﹌身腐败 2024-09-26 12:54:23

根据 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文