黑马程序员-内省,javabean

---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------

JavaBean:特殊的java,主要用于传递数据信息,这种java类中的方法主要用于访问私有字段,且方法名符合某种命名规则。

列如对age属性的操作:int getAge()   , void  setAgeint age

一般根据方法名称就可以判断出javabean中有哪些属性

规则:列如Age--->如果第二个字母是小的,则把第一个字母变成小的--->age

   getTime---->time

   gettime---->time

   getCPU--->cpu

如果要在两个模块之间传递多个对象,可以将这些信息封装到一个JavaBean中,这种JavaBean的实例对象通常称之为(Value Object 简称VO),总之,一个类被当做JavaBean使用时,Javabean的属性石根据方法名推断出来的,它根本看不到java类内部的成员变量

   一个符号JavaBean特点的类可以当作普通类一样进行使用,但把它当作JavaBean使用肯定会带来额外的好处:

   主要好处:JDK中提供了对JavaBean进行操作的一些API,这套API就称之为内省,

 

Person类(自定义的一个JavaBean

public class Person {

	private String name;
	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
}

IntorspctionReflect

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

public class IntrospectionReflect {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		Person p = new Person("zhansan", 10);
		String propertieName = "name";
		// 如果要利用反射来获取Person对象中的name字段的名字 ,该如何操作呢?
		//
		/*
		 * Method method=p.getClass().getMethod("getName",null);
		 * System.out.println(method.invoke(p, null));
		 */
		// 如果我们用JDK提供的API ,也就是内省
		PropertyDescriptor pd = new PropertyDescriptor(propertieName,
				p.getClass());
		Method method = pd.getReadMethod();
		System.out.println(method.invoke(p, null));

	}

}

其实个人感觉用内省来操作,并不比原来简单

//现在用内省给Person对象set一个值

Method method1 = pd.getWriteMethod();
		method1.invoke(p, "lisi");
		// 介绍一种较为复杂的方式:用Introspector来完成
		BeanInfo beanInfo = Introspector.getBeanInfo(p.getClass());
		PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
		for (PropertyDescriptor pd1 : pds) {
			if (pd1.getName().equals(propertieName)) {
				Method method = pd.getReadMethod();
				System.out.println(method.invoke(p, null));
				break;
			}
		}

利用BeanUtils工具包来操作JavaBean

BeanUtils工具包是第三方提供的(Apache),我们需要从网上下载下来才可以使用

导入jar包:

   点击src---->新建Folderbin---->jar包考进去---->(右键)Add to BuildPath

 

 

写代码:Person p=new Person("zhansan",10);

String propertieName="name";

//利用BeanUtils工具包来操作

String bu=BeanUtils.getProperty(p, propertieName);

System.out.println(bu);

结果提示一下错误:

 

因为我们虽然导入了BeanUtilsjar包,但是BeanUtils引用了其他外部jar包,所以我们还得继续导入loggingjar

 

然后就可以了

也可以用BeanUtils来设置

//利用BeanUtils工具包来操作

BeanUtils.setProperty(p, propertieName, "lisi");

BeanUtils支持属性的级联操作

列如在Person类中增加Date属性:

private Date birthday = new Date();// 注意必须初始化

	public Date getBirthday() {
		return birthday;
	}

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

IntrospectionReflect类 中

BeanUtils.setProperty(p, "birthday.time""123");

String birthday=BeanUtils.getProperty(p, "birthday.time");

还可以把JavaBean的属性转换成一个Map,也可以把一个Map转换成Javabean

基于上述原因,我们其实可以这样操作Map

BeanUtils包中,还有一个类PropertyUtils

比较其用:

BeanUtils.setProperty(p, "age""123");

System.out.println(BeanUtils.getProperty(p, "age"));//返回类型为String

PropertyUtils.setProperty(p, "age", 10);

System.out.println(PropertyUtils.getProperty(p, "age"));//返回类型为int

BeanUtilsPropertyUtils都可以操作JavaBean的属性,但是这两者有什么不同呢?

从上面代码可以看出:在Person类中定义的age的数据类型为int ,但是在用BeanUtils操作时,都是看做String来操作的,返回类型也是String,而用PropertyUtils时,就是int类型的


---------------------<a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ---------------------






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值