关于JavaBean命名

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于JavaBean命名相关的知识,希望对你有一定的参考价值。

Given the following six method names:
addListener
addMouseListener
setMouseListener
deleteMouseListener
removeMouseListener
registerMouseListener
How many of these method names follow JavaBean Listener naming rules?

如题,到底有几个?为什么我看着觉得这6个都差不多

JavaBean Listener Naming Rules
Listener method names used to "register" a listener with an event source must use the prefix add, followed by the listener type. For example,addActionListener() is a valid name for a method that an event source will have to allow others to register for Action events.
Listener method names used to remove ("unregister") a listener must use the prefix remove, followed by the listener type (using the same rules as the registration add method).
The type of listener to be added or removed must be passed as the argument
Listener method names must end with the word "Listener".

所以listener的规则是添加是add 删除是remove并且要跟上listener的具体类型
所以是两个
addMouseListener和removeMouseListener
参考技术A 看了下面的全迷糊了,翻书看看去 参考技术B setMouseListener,JavaBean只能有以set和get开头的方法。

解密:普通JavaBean的Property(域/类变量)的命名规则

转自:https://www.iteye.com/blog/pouyang-634844

Java属性命名规范!
一般情况下。Java的属性变量名都已小写字母开头,如:userName,showMessage等,但也存在着特殊情况,考虑到一些特定的有意思的英文缩略词如(USA,XML等),JavaBean也允许大写字母起头的属性变量名,不过必须满足“变量的前两个字母要么全部大写,要么全部小写”的要求,如:IDCode、ICCard、idCode等属性变量名是合法的,而iC、iCcard、iDCode等属性变量名是非法的。正是由于这个原因造成了下面这种情况:
举个例子:
JavaBean

  Java代码  技术图片

public class RegionDTO  implements Serializable{  
    public String cId;  
    public String getCid() {  
        return cid;  
    }  
    public void setCid(String cid) {  
        this.cid = cid;  
    }  
    public String cName;      
      
    public String getCName() {  
        return cName;  
    }  
    public void setCName(String name) {  
        cName = name;  
    }   
}  

封装成List后,然后在页面上用C标签进行显示

  Java代码  技术图片

${item.cId}//报错 RegionDTO没有这个属性!!!  

转自:http://chanson.iteye.com/blog/419028

1、背景

本文讲的普通JavaBean只是一个拥有Property(域/类变量)及其setter/getter的普通Java类。

有一定Java开发经验的人可能会知道,普通JavaBean的Property(域/类变量)的命名不能采用以下形式:aA***或者Aa***,如:"aDdress"或"Address",否则,在web应用中会报无法找到这个Property(因为根据"规则",需要找的是"ADdress"或"address")。但对于其中的原因,一般人都不明白,难道这是Sun公司当初定的规范吗?

Java开源以后,我们终于可以解开其中的谜:

 

2、普通JavaBean处理涉及到相关类

在web应用中,Servlet容器或者EJB容器一般会使用java.beans包中的类来加载这些JavaBean。

BeanInfo(接口)
|
SimpleInfo(类)
|
GenericBeanInfo(类)

GenericBeanInfo是JavaBean数据装载类。
Introspector是JavaBean处理中最重要的一个处理类。
另外的一些辅助类,就不一一列举了。

 

3、解密

3.1 开始

在应用中,我们通常会用以下代码来获取一个普通JavaBean相关的信息:
BeanInfo mBeanInfo = null;
try {
mBeanInfo = Introspector.getBeanInfo(Person.class);
} catch (IntrospectionException e) {
e.printStackTrace();
}

3.2 深入
在Introspector类的getBeanInfo方法中,我们发现其中与Property处理相关的行:
private GenericBeanInfo getBeanInfo()
throws IntrospectionException {
……
PropertyDescriptor apropertydescriptor[] = getTargetPropertyInfo();
……
}

3.3 继续深入
在Property处理方法中,我们发现其处理方式是根据getter/setter的方法来得到Property(域/类变量)
private PropertyDescriptor[] getTargetPropertyInfo() throws IntrospectionException{
……
if(s.startsWith("get")) obj = new PropertyDescriptor(decapitalize(s.substring(3)), method, null);
……
}

3.4 关键
接下来,最关键的就是下面这个方法:
public static String decapitalize(String s)
{
if(s == null || s.length() == 0)
//空处理
return s;

if(s.length() > 1 && Character.isUpperCase(s.charAt(1)) && Character.isUpperCase(s.charAt(0))){
//长度大于1,并且前两个字符大写时,返回原字符串
return s;
} else{
//其他情况下,把原字符串的首个字符小写处理后返回
char ac[] = s.toCharArray();
ac[0] = Character.toLowerCase(ac[0]);
return new String(ac);
}
}

4 Ending
解密完成。
明白真相,才会解开心中的锁——其实很简单。

以上是关于关于JavaBean命名的主要内容,如果未能解决你的问题,请参考以下文章

关于javabean中乱码的疑问

java面试题5 牛客:下列关于JavaBean的说法正确的是:

关于Java中内省的总结

关于flexjson将json转为javabean的使用

关于如何在Tomcat中使用JavaBean

关于bean.java的简单介绍!