黑马程序员:内省(IntroSpector)与JavaBean的介绍

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

内省(IntroSpector) --> javaBean(一个特殊的Java类,它的方法要按某种规则来取名,int getAge(), void setAge(int age), get(有返回值, 无参数),set(无返回值, 有参数)开头命名的方法,且都是公有的)

示例:
class Person {
	private int x;
	public int getAge() {
		return x;
	}
	public void setAge(int age) {
	 this.x = age;
	}
}

若把Person当作JavaBean类来看,则应该有age变量,而不是x变量,因为JavaBean是通过观察get和set方法来猜测变量的,私有的外部是看不到的。

如上例中,应该有Age变量,而这个变量写法上的规范是:
如果第二个字母是小写的,则把第一个字母变成小写 Age->age;
方法叫gettime --> JavaBean的变量名应该是time;
setTime --> time
getCPU  --> CPU

JavaBean是一种特殊的Java类,主要用于传递数据信息,这种java类中的方法主要用于访问私有的字段,且方法名符合某种命名规则。
如果要在两个模块之间传递多个信息,可以将这些信息 封装到一个JavaBean中,这种JavaBean的实例对象通常称为值对象(Value Object 简称VO)。这些信息在类中用私有字段来存储,如果读取或者设置这些字段的值,则需要通过一些相应的方法来访问。JavaBean的变量名是根据其中的setter和getter方法来确定的,而不是根据其中的成员变量。 

对JavaBean的简单内省操作:
public class Test {
	public static void main(String[] args) throws Exception{
	ReflectPoint rf = new ReflectPoint(3,5);
	//导出关于x变量的方法
	PropertyDescriptor props = new PropertyDescriptor("x",rf.getClass());
	//获取x变量的读方法,即getX()
	Method methodGetX = props.getReadMethod();
	Object retVal = methodGetX.invoke(rf);
	System.out.println(retVal);
	//获取x变量的写方法,即setX()
	Method methodSetX = props.getWriteMethod();
	methodSetX.invoke(rf, 7);
	System.out.println(rf.getX());
	}
}
//JavaBean类
class ReflectPoint {
	private int x;
	private int y;
	ReflectPoint(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;
	}


	public static void main(String[] args) {


	}
}

为了提高代码编写能力,下面看一种复杂一点的内省操作
采用便利BeanInfo的所有属性方法来查找和设置某个对象中的x属性。在程序中,把一个类当作JavaBean来看,就是调用IntroSpector.getBeanInfo方法,得到BeanInfo对象,该对象封装了把这个类当作JavaBean看的结果信息

public class Test {
	public static void main(String[] args) throws Exception{
		ReflectPoint rf = new ReflectPoint(3,5);
		String propertyName = "x";
		//获取x变量的读方法,即getX()
		Object retVal = getProperty(rf, propertyName);
		System.out.println(retVal);
		//获取x变量的写方法,即setX()
		int value = 7;
		setProperty(rf, propertyName, value);
		System.out.println(rf.getX());
	}


	private static void setProperty(ReflectPoint rf, String propertyName,
		int value) throws IntrospectionException, IllegalAccessException,
		InvocationTargetException {
	PropertyDescriptor props = new PropertyDescriptor(propertyName,rf.getClass());
	Method methodSetX = props.getWriteMethod();
	methodSetX.invoke(rf, value);
}
	private static Object getProperty(ReflectPoint rf, String propertyName)
		throws IntrospectionException, IllegalAccessException,
		InvocationTargetException {
		//导出关于x变量的方法
		//PropertyDescriptor props = new PropertyDescriptor(propertyName,rf.getClass());


//		Method methodGetX = props.getReadMethod();
//		Object retVal = methodGetX.invoke(rf);


		//内省的另一种稍微复杂一点的方式 这里举例的方法为getX()
		BeanInfo beanInfo = Introspector.getBeanInfo(rf.getClass()); //获取BeanInfo对象,即把rf.getClass()当成JavaBean类来看
		PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); //获取BeanInfo对象中所有属性信息
		Object retVal = null;
		for(PropertyDescriptor pd : pds) {
			if(pd.getName().equals(propertyName)) {
				Method methodGetX = pd.getReadMethod();
				retVal = methodGetX.invoke(rf);
				break;
			}
		}	
			return retVal;
	}
}


class ReflectPoint {
	private int x;
	private int y;
	ReflectPoint(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;
	}


	public static void main(String[] args) {


	}
}

---------------------- 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、付费专栏及课程。

余额充值