`

Java基础五:内省(对JavaBean的操作)

阅读更多
/**
 * Person JavaBean
 * 
 * JavaBean的要求:有get,set方法,JavaBean的属性值是根据get,set方法后面的内容来确定的。
 * 
 * @author 张明学
 * 
 */
public class PersonBean {
	private String name;
	private int age;
	private Date birthday = new Date();
	private String x;
	public PersonBean() {
	}
	public PersonBean(String name, int age) {
		this.name = name;
		this.age = age;
	}
	public String getP() {
		return x;
	}
	public void setP(String p) {
		this.x = p;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

 注意上面的x属性和getP及setP方法

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

/**
 * 内省(对JavaBean的操作)
 * 
 * @author 张明学
 * 
 */
public class IntroSpectorStudy {

	public static void main(String[] args) throws Exception {
		PersonBean pb = new PersonBean("java", 23);
		pb.setP("p属性");
		// 定义几个属性名称
		String propertyP = "p";
		String propertyName = "name";
		String propertyAge = "age";

		// **************PropertyDescriptor的使用**************\\
		// 使用内省
		Object returnValue = null;
		// 获取age属性
		returnValue = getProperty(pb, propertyAge);
		System.out.println(returnValue);
		// 获取p属性(在PersonBean中只有getP,setP方法,没有P变量,getP,setP操作的是x变量,它的JavaBan属性名是p)
		returnValue = getProperty(pb, propertyP);
		System.out.println(returnValue);

		String newValue = "C++";
		// 设置name属性
		setProperty(pb, propertyName, newValue);
		System.out.println(pb.getName());

		// **************BeanInfo的使用**************\\
		// 其它办法:
		BeanInfo beanInfo = Introspector.getBeanInfo(PersonBean.class);
		// PersonBean所有的属性
		PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
		for (int i = 0; i < pds.length; i++) {
			System.out.println("--------属性名:" + pds[i].getName() + "--------");
			System.out.println("属性类型:" + pds[i].getPropertyType());
			System.out.println("get方法:" + pds[i].getReadMethod());
			System.out.println("set方法:" + pds[i].getWriteMethod());
		}

	}

	/**
	 * 获取JavaBean的属性值
	 * 
	 * @param bean
	 *            JavaBean对象
	 * @param propertyName
	 *            属性名称
	 * @return 该JavaBean对象的属性值
	 * @throws Exception
	 */
	private static Object getProperty(PersonBean bean, String propertyName)
			throws Exception {
		Object returnValue = null;
		PropertyDescriptor pd = new PropertyDescriptor(propertyName,
				PersonBean.class);
		Method methodGetAge = pd.getReadMethod();
		returnValue = methodGetAge.invoke(bean, null);
		return returnValue;
	}

	/**
	 * 设置JavaBean的属性值
	 * 
	 * @param bean
	 *            JavaBean对象
	 * @param propertyName
	 *            属性名
	 * @param propertyValue
	 *            设置的属性值
	 * @throws Exception
	 */
	private static void setProperty(PersonBean bean, String propertyName,
			String propertyValue) throws Exception {
		PropertyDescriptor pd;
		pd = new PropertyDescriptor(propertyName, PersonBean.class);
		Method methodSet = pd.getWriteMethod();
		methodSet.invoke(bean, propertyValue);
	}
}

 

23
p属性
C++
--------属性名:age--------
属性类型:int
get方法:public int com.mengya.PersonBean.getAge()
set方法:public void com.mengya.PersonBean.setAge(int)
--------属性名:birthday--------
属性类型:class java.util.Date
get方法:public java.util.Date com.mengya.PersonBean.getBirthday()
set方法:public void com.mengya.PersonBean.setBirthday(java.util.Date)
--------属性名:class--------
属性类型:class java.lang.Class
get方法:public final native java.lang.Class java.lang.Object.getClass()
set方法:null
--------属性名:name--------
属性类型:class java.lang.String
get方法:public java.lang.String com.mengya.PersonBean.getName()
set方法:public void com.mengya.PersonBean.setName(java.lang.String)
--------属性名:p--------
属性类型:class java.lang.String
get方法:public java.lang.String com.mengya.PersonBean.getP()
set方法:public void com.mengya.PersonBean.setP(java.lang.String)

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics