Confused about naming of JavaBean properties, with respect to getters and setters

情到浓时终转凉″ 提交于 2019-11-29 11:01:01
frm

The JavaBeans Specification says that for a property propertyName there should be a getter method getPropertyName() and/or a setter method setPropertyName().

A property is defined by the only presence of the getter and setter methods and can also be a computed value. A instance variable on the object is not required.

The specification defines the capitalization rules for properties and getter/setter methods:

Thus when we extract a property or event name from the middle of an existing Java name, we normally convert the first character to lower case. However to support the occasional use of all upper-case names, we check if the first two characters of the name are both upper case and if so leave it alone. So for example,

“FooBah” becomes “fooBah”, “Z” becomes “z”, “URL” becomes “URL


The method is in fact implemented as:

/*
Utility method to take a string and convert it to normal Java variable name 
capitalization. This normally means converting the first character from upper case to  
lower case, but in the (unusual) special case when there is more than one character  
and both the first and second characters are upper case, we leave it alone.

Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays as "URL".

 Parameters:
     name The string to be decapitalized.
 Returns:
 The decapitalized version of the string.
 */
 public static String  decapitalize(String name) {
 if (name == null || name.length() == 0) {
 return name;
 }
 if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
                     Character.isUpperCase(name.charAt(0))){
        return name;
     }

     char chars[] = name.toCharArray();
     chars[0] = Character.toLowerCase(chars[0]);
     return new String(chars);
 }

So:

  1. if the name is null, return it as such
  2. if the name has first two characters in caps, return it as such
  3. all other strings, decapitalize first character
Vadim

That's defined by the JavaBeans naming conventions. The getter name will have "get" followed by the property name with the first letter capitalized.

A related question with more information

When javabeans refer to a "property" it is something with a get() and a set()-method. It doesn't care what the internal storage of data is (if there even is one).

Thus a property "foo" has access methods getFoo() and setFoo(), what these methods do is irrelevant to the definition of the property.

http://docs.oracle.com/javase/tutorial/javabeans/writing/properties.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!