关于属性名的部分:
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”.
工具翻译+个人修改:
工具方法把一个字符串并将其转换为正常的Java变量名的大写。这通常意味着将第一个字符从大写转换为小写,
但在(特殊)的特殊情况下,当前两个字符或多个字符是大写时,我们不处理他(直接返回)。因此,“Foobah”变成了“foobah”和“X”变成了“x”,但“URL”仍然是“URL”。
java.bean.Introspector#decapitalize
这个方法的javadoc注释。
方法如下:
/**
* 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.
* <p>
* Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays
* as "URL".
*
* @param name The string to be decapitalized.
* @return The decapitalized version of the string.
*/
publicstatic String decapitalize(String name) {
if(name == null|| name.length() == 0) {
returnname;
}
if(name.length() > 1&& Character.isUpperCase(name.charAt(1)) &&
Character.isUpperCase(name.charAt(0))){
returnname;
}
charchars[] = name.toCharArray();
chars[0] = Character.toLowerCase(chars[0]);
returnnew String(chars);
}
我们使用这个方法对几种不同的字段名进行测试:
看下面的代码:
publicclass TestBean {
publicstatic void main(String[] args) {
System.out.println(Introspector.decapitalize("name"));
System.out.println(Introspector.decapitalize("Name"));
System.out.println(Introspector.decapitalize("mName"));
System.out.println(Introspector.decapitalize("NAME"));
}
}
输出:
name
name
mName
NAME
name
和Name
的结果是一样的,也就是说如果字段名是Name
,会被当成name
使用,因此从这个角度来说这种命名方式是错误的,不符合规范的。
mName是没问题问题的。NAME也没有问题,NAme,NAMe这样也不会有错。