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

黑马程序员_基础加强之JavaBean

2014-06-18 03:15 453 查看
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
  
  JavaBean是一种特殊的Java类,主要用于传递数据,JavaBean的方法主要用于访问私有字段。JavaBean的方法名必须符合某种命名规则。

  如果要在两个模块之间传递多个信息,可以将这些信息封装到一个JavaBean中,这种JavaBean实例对象称为值对象(Value Object,简称VO)。这些信息在类中用私有字段来存储,若要读取或设置这些字段值,则需要通过一些对应的方法来访问,JavaBean的属性是根据setter和getter方法来确定的。

  如果方法名为setId,即设置id。如果方法名为getId,即获取id。去掉set前缀,剩余部分就是属性名,如果剩余部分的第二个字母是小写的,则把剩余部分的首字母改成小的。

  setGPU的属性名GPU

  总之,一个类被当作javaBean使用时,JavaBean属性是根据方法名推出来的,因为JavaBean根本看不到java类内部的成员变量。

  一个符合JavaBean特点的类可以当作普通类一样进行使用,但把它当JavaBean用时,有一些额外的好处:

  在Java EE开发中,经常要使用到JavaBean。很多环境就要求按JavaBean方式操作,别人都这么用和要求这么做,这是约定俗成的,最好遵守。

  JDK中提供了对JavaBean进行操作的一些API,这套API就称为内省。如果要你自己去通过getX方法来访问私有的x,有一定难度,用内省这套api操作JavaBean比用普通类的方式更方便。

  

package cn.itcast.day1;

import java.util.Date;

/*
* JavaBean,一个特殊的java类
*/
public class ReflectPoint {
//属性是根据setter和getter方法来确定的
private Date birthday = new Date();

private int x;
public int y;
public String str1 = "ball";
public String str2 = "basketball";
public String str3 = "itcast";

public ReflectPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final ReflectPoint other = (ReflectPoint) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}

@Override
public String toString(){
return str1 + ":" + str2 + ":" + str3;
}

/*
* 有setter和getter方法,方法名符合某种规则
*/

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

}
package cn.itcast.day1;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;

public class IntroSpectorTest {

/*
* 演示用eclipse自动生成 ReflectPoint类的setter和getter方法。
直接new一个PropertyDescriptor对象的方式来让大家了解JavaBean API的价值,先用一段代码读取JavaBean的属性,然后再用一段代码设置JavaBean的属性。
演示用eclipse将读取属性和设置属性的流水帐代码分别抽取成方法:
只要调用这个方法,并给这个方法传递了一个对象、属性名和设置值,它就能完成属性修改的功能。
得到BeanInfo最好采用“obj.getClass()”方式,而不要采用“类名.class”方式,这样程序更通用。
采用遍历BeanInfo的所有属性方式来查找和设置某个RefectPoint对象的x属性。在程序中把一个类当作JavaBean来看,就是调用IntroSpector.getBeanInfo方法, 得到的BeanInfo对象封装了把这个类当作JavaBean看的结果信息。

*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ReflectPoint pt1 = new ReflectPoint(3,5);

String propertyName = "x";
//"x"-->"X"-->"getX"-->MethodGetX-->
Object retVal = getProperty(pt1, propertyName);//pt1上propertyName的值,即getX的值
System.out.println(retVal);

Object value = 7;

setProperties(pt1, propertyName, value);//设置pt1上propertyName的值为value,即setX=7
/*
* BeanUtils:对JavaBean进行操作的工具类
* 对JavaBean pt1使用BeanUtils取x的值
* 取得的值是String类型
*/
System.out.println(BeanUtils.getProperty(pt1, "x").getClass().getName());
/*
* 将pt1上的属性x的值设为9
* 注意setProperty中存入的值是String类型
*/
BeanUtils.setProperty(pt1, "x", "9");
System.out.println(pt1.getX());
/*
//java7的新特性
Map map = {name:"zxx",age:18};
BeanUtils.setProperty(map, "name", "lhm");
*/
BeanUtils.setProperty(pt1, "birthday.time", "111");
System.out.println(BeanUtils.getProperty(pt1, "birthday.time"));

PropertyUtils.setProperty(pt1, "x", 9);
System.out.println(PropertyUtils.getProperty(pt1, "x").getClass().getName());

}

private static void setProperties(Object pt1, String propertyName,
Object value) throws IntrospectionException,
IllegalAccessException, InvocationTargetException {

/*
* 为符合标准 Java 约定的属性构造一个 PropertyDescriptor。
* propertyName - 属性名。
beanClass - 目标 bean 的 Class 对象。 cn.itcast.day1.ReflectPoint.class
*/
//创建一个属性描述符,指定属性名和类(把哪个类当成JavaBean看,就指定那个类)
PropertyDescriptor pd2 = new PropertyDescriptor(propertyName,pt1.getClass());
//获取它的setX方法
Method methodSetX = pd2.getWriteMethod();
//调用setX方法
methodSetX.invoke(pt1,value);
}

private static Object getProperty(Object pt1, String propertyName)//pt1 x
throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
/*PropertyDescriptor pd = new PropertyDescriptor(propertyName,pt1.getClass());
获得getX方法
Method methodGetX = pd.getReadMethod();
Object retVal = methodGetX.invoke(pt1);*/

//得到BeanInfo对象,这个对象封装了把ReflectPoint当做JavaBean看的结果信息
BeanInfo beanInfo =  Introspector.getBeanInfo(pt1.getClass());
//得到所有的属性描述
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
Object retVal = null;
for(PropertyDescriptor pd : pds){
//属性名与传进来的属性名相同
if(pd.getName().equals(propertyName))
{
//取出get方法
Method methodGetX = pd.getReadMethod();
//调用get方法
retVal = methodGetX.invoke(pt1);
break;
}
}
return retVal;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: