黑马程序员-----JavaBean的内省操作

---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------

1.JavaBean
JavaBean是一个特殊的Java类,一个标准的JavaBean有以下一个特性:
--> JavaBean是一个公共类。
--> JavaBean有一个不带参数的构造方法。
--> JavaBean通过setXXX方法设置属性,通过getXXX方法获取属性。

2.内省和PropertyDescriptor
内省(IntroSpector)是Java 语言对 Bean 类属性、事件的一种缺省处理方法。
PropertyDescriptor描述JavaBean通过一对存储器方法导出的一个属性。 主要用于描述JavaBean的属性。

构造器:
PropertyDescriptor(String propertyName, Class<?> beanClass):通过调用getFoo和setFoo存取方法,为符合标准Java约定的属性构造一个PropertyDescriptor。propertyName代表属性名称,beanClass代表目标bean的Class对象。

主要方法:
Class<?> getPropertyType():获得属性的Class对象。
Method getReadMethod():获得应该用于读取属性值的方法。
Method getWriteMethod():获得应该用于写入属性值的方法。
public class Point {

	//属性
	private int x;
	private int y;
	
	//构造器
	public Point(){}
	public Point(int x, int y) {
		this.x = x;
		this.y = y;
	}

	//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 class IntroSpectorDemo {

	public static void main(String[] args) throws Exception {

		Point pt = new Point(3,5);
		
		//下面使用内省对Point对象属性值进行操作
		
		//PropertyDescriptor
		PropertyDescriptor dp1 = new PropertyDescriptor("x", pt.getClass());
		//获取属性x的值
		Method getXMethod = dp1.getReadMethod();
		Object retVal = getXMethod.invoke(pt);
		System.out.println(retVal);
		
		//PropertyDescriptor
		PropertyDescriptor dp2 = new PropertyDescriptor("y", pt.getClass());
		//改变属性y的值
		Method setYMethod = dp2.getWriteMethod();
		setYMethod.invoke(pt, 7);
		System.out.println(pt.getY());
		
	}

}

3.BeanInfo和Introspector
3.1 BeanInfo
Introspector类是对JavaBean进行内省操作的类。
它的静态方法getBeanInfo返回一个BeanInfo对象。

static BeanInfo getBeanInfo(Class<?> beanClass):在 Java Bean 上进行内省,了解其所有属性、公开的方法和事件。

3.2 Introspector
BeanInfo类是提供有关其 bean 的方法、属性、事件等显式信息的类。

主要方法:
MethodDescriptor[] getMethodDescriptors(): 获得 beans MethodDescriptor
PropertyDescriptor[] getPropertyDescriptors():获得 beans PropertyDescriptor。

例:
存在一个JavaBean,它包含以下几种可能的属性:
1:boolean/Boolean
2:int/Integer
3:String
4:double/Double属性名未知,
现在要给这些属性设置默认值,以下是要求的默认值:
String类型的默认值为字符串   www.itheima.com 
int/Integer类型的默认值为100 
boolean/Boolean类型的默认值为true
double/Double的默认值为0.01D.
只需要设置带有getXxx/isXxx/setXxx方法的属性,非JavaBean属性不设置,请用代码实现。
//JavaBean
class MyBean{
	
	//属性
	private boolean b;
	private int i;
	private String s;
	private double d;
	
	//构造器
	public MyBean(){}

	//setter和getter
	public boolean isB() {
		return b;
	}

	public void setB(boolean b) {
		this.b = b;
	}

	public int getI() {
		return i;
	}

	public void setI(int i) {
		this.i = i;
	}

	public String getS() {
		return s;
	}

	public void setS(String s) {
		this.s = s;
	}

	public double getD() {
		return d;
	}

	public void setD(double d) {
		this.d = d;
	}
}

public class Test6 {

	public static void main(String[] args) throws Exception {

		//得到目标类的Class
		Class clazz = Class.forName("com.itheima.MyBean");
		//创建对象
		Object bean = clazz.newInstance();
		//得到该类的BeanInfo
		BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
		//获得 beans PropertyDescriptor
		PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
		//遍历
		for(PropertyDescriptor descriptor : descriptors){
			//获取属性名
			String name = descriptor.getName();
			//获取属性类型
			Class propertyType = descriptor.getPropertyType();
			//获取set方法
			Method setMethod = descriptor.getWriteMethod();
			//获取get方法
			Method getMethod = descriptor.getReadMethod();
			
			//如果属性名非class
			if(!"class".equals(name)){
				//如果set方法不为空
				if(setMethod != null){
					//如果属性类型是Boolean或boolean
					if(propertyType == Boolean.class || propertyType == boolean.class)
						//赋值
						setMethod.invoke(bean, true);
					//如果属性类型是Integer或int
					if(propertyType == Integer.class || propertyType == int.class)
						setMethod.invoke(bean, 100);
					//如果属性类型是String
					if(propertyType == String.class)
						setMethod.invoke(bean, "www.itheima.com");
					//如果属性类型是Double或double
					if(propertyType == Double.class || propertyType == double.class)
						setMethod.invoke(bean, 0.01D);
				}
				//如果get方法不为空
				if(getMethod != null)
					System.out.println(name + "属性值:" + getMethod.invoke(bean));
			}
					
		}
	}
}

4.使用BeanUtils操作JavaBean
BeanUtils工具包是专门用来操作JavaBean的工具类。
使用BeanUtils可以直接使用setProperty和getProperty方法对上面代码进行简化
BeanUtils.getProperty(pt, "x");
BeanUtils.setProperty(pt, "y", "9");



---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------详细请查看: http://edu.csdn.net
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值