您的位置:首页 > 职场人生

黑马程序员_java高新技(5)JavaBean

2013-03-17 21:46 211 查看
------- android培训java培训、期待与您交流! ----------

JavaBean是特殊的java类

private int x;

public int getAge()

{

     return x;

}

public void setAge(int age)

{

     this.x=age;

}

javaBean设置age属性

javbean的属性名去掉get和set,然后把首字母改成小写的(前提是第二个字母是小写的)

先生成get,set方法,然后用javaBean

方法获取

属性描述

Propertyscriptor pd=new Propertyscriptor(属性名,pt1.class())

然后就会得到get,set方法

Method methodGetX=pd.getReadMethod();//获取了读方法

然后调用 找准对象

Object retVal=methodGetX.invoke(pt1);

eclipse有一个重构代码的方法,可以抽去方法。

BeanInfo beanInfo=Introspector.getBeanInfo(pt1.getClass());

PropertyDescriptor[] pds=beanInfo.getPropertyDescriptors();

 迭代出所有属性名

for(PropertyDescriptor pd:pds)

{

     if(pd.getName().equlas(propertyName))

     {

        Mehod methodGetX=pd.getReadMethod();

        retVal=methodGetX.invoke(pt1);

        break;

    }

}//可以遍历出所有的属性名

return retVal;

javaBeanutils

BeanUtils.getProperty(pt1,"x");//拿值

BeanUtils.setProperty(pt1,"x","9");设置值。//用字符串类型进行操作

Map map=(name:"zzz",age:18);

BeanUtils.setsetProperty(map,"name","lhm");

PropertyUtils.setProperty(pt1,"x",9);//以属性本身的类型进行操作
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: