本文介绍了Java:访问者的命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找Java中有关访问器的正式命名约定.

I'm looking for the official naming convention in Java regarding accessors.

例如,我已经看到JPanel类不赞成使用size()方法,而推荐使用getSize().

I've seen that, for instance, the JPanel class deprecated the size() method in favor of getSize().

但是在ArrayList类中,方法是size().

所以我想知道访问器应该命名为getXXX()还是xXX()吗?

So I'm wondering if accessors should be named getXXX() or xXX() ?

推荐答案

使用 JavaBeans约定(获取器和设置器)通常是个坏主意. >许多框架通过反思来使用它们,尤其是在 EL 中,在某些情况下,有时没有权限获取者(取决于EL的风格),您将无法访问字段.

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).

因此,您的访问器应始终命名为getXxx()isXxx()setXxx().

So your accessors should always be named getXxx() or isXxx() and setXxx().

size()是瑕疵"的一个示例,该瑕疵可能会惹恼开发人员(请参阅下面的链接).乔什·布洛赫(Josh Bloch)和尼尔·戈夫特(Neal Gafter)为使其更具可读性而做出的选择现在使得在某些情况下(EL)很难获得.

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).

但是请记住,JavaBeans约定不是Java命名约定.

But remember the JavaBeans convention isn't the Java naming convention.

资源:

  • How to Access the Size of a Collection in a JSP Page Using JSTL-EL
  • Java conventions
  • JavaBeans
  • JavaBeans specification

关于同一主题:

这篇关于Java:访问者的命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 18:54