黑马程序员————javaBean 与 内省


------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------


javaBean是一个特殊的java类。


通过PropertyDescriptor类获得get和set方法。

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


public class JavaBeanTest {
	public static void main(String[] args)throws Exception{
		P p=new P(2,3);
		String propertyName="x";
		//"x"-->"X"-->"getX"--->MethodGetX-->
		PropertyDescriptor pd=new PropertyDescriptor(propertyName,p.getClass());
		Method methodGetX=pd.getReadMethod();//只读
		Object retVal=methodGetX.invoke(p);
		System.out.println(retVal);
		
		Method methodSetX=pd.getWriteMethod();//只写
		methodSetX.invoke(p,9);
		System.out.println(p.getX());
	}
}
class P
{
	public int x;
	public int y;
	public P(int x,int y){
		this.x=x;
		this.y=y;
	}
	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;
	}
	
}

当把该类当作javaBean看待时,可以直接调用IntroSpector.getBeanInfo的方法,得到的BeanInfo对象封装了这个类当作javaBean看的结果信息

实例:

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
public class JavaBeanTest2 {
	public static void main(String[] args)throws Exception{
		String propertyName ="x";
		P p=new P(2,3);
		BeanInfo beanInfo=Introspector.getBeanInfo(p.getClass());
		PropertyDescriptor[] pds=beanInfo.getPropertyDescriptors();
		Object retVal=null;
		for(PropertyDescriptor pd:pds){
			if(pd.getName().equals(propertyName))
			{
				Method methodGetX=pd.getReadMethod();
				retVal=methodGetX.invoke(p);
			}
		}
		System.out.println(retVal);
	}
}
class P
{
	public int x;
	public int y;
	public P(int x,int y){
		this.x=x;
		this.y=y;
	}
	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;
	}
	
}

使用BeanUtils工具包操作JavaBean。

BeanUtils是以String形式进行操作。

PropertyUtils是以自己本身的类型操作。

这都需要加入工具包。引入外部jar。

commons-beanutils-1.9.2.jar

commons-logging-1.2.jar


代码展示:

import java.util.Date;

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

public class JavaBeanTest2 {
	public static void main(String[] args) throws Exception {
		P p = new P(2, 3);
		System.out.println(BeanUtils.getProperty(p, "x"));
		System.out.println(BeanUtils.getProperty(p, "x").getClass().getName());
		//BeanUtils是String类型操作
		BeanUtils.setProperty(p, "x", "7");
		System.out.println(p.getX());
		
	}
}

class P {
	public int x;
	public int y;

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

	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;
	}

}

运行出错:


解决办法:

不能把所要访问的类放在同一个class文件,因该单独写个class类,并访问权限是public 

Pott.java

public class Pott {
	public int x;
		public int y;
		public Pott(int x,int y){
			this.x=x;
			this.y=y;
		}
		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;
		}
		
	}

JavaBeanTest2.java

import java.util.Date;

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

public class JavaBeanTest2 {
	public static void main(String[] args) throws Exception {
		Pott p = new Pott(2, 3);
		System.out.println(BeanUtils.getProperty(p, "x"));
		System.out.println(BeanUtils.getProperty(p, "x").getClass().getName());
		//BeanUtils是String类型操作
		BeanUtils.setProperty(p, "x", "7");
		System.out.println(p.getX());
		
		//PropertyUtils是以自己本事类型操作的
		PropertyUtils.setProperty(p, "x", 9);
		System.out.println(PropertyUtils.getProperty(p, "x").getClass().getName());
	}
}


修改后实际运行结果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值