标签:http io os 使用 ar java sp 问题 on
JavaBean规范文档:http://download.oracle.com/otndocs/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/
关于属性名的部分:
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. */ 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); }
我们使用这个方法对几种不同的字段名进行测试:
看下面的代码:
public class TestBean { public static 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这样也不会有错。
这篇文章说深入可能还不够资格,并且标题中的setter和getter根本没提。如果有不明白的地方,欢迎留言!
深入了解JavaBean规范中的属性名和setter/getter方法
标签:http io os 使用 ar java sp 问题 on
原文地址:http://my.oschina.net/flags/blog/326752