---------------------- <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 setAge(int 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---->新建Folder(bin)---->将jar包考进去---->(右键)Add to BuildPath
写代码:Person p=new Person("zhansan",10);
String propertieName="name";
//利用BeanUtils工具包来操作
String bu=BeanUtils.getProperty(p, propertieName);
System.out.println(bu);
结果提示一下错误:
因为我们虽然导入了BeanUtils的jar包,但是BeanUtils引用了其他外部jar包,所以我们还得继续导入logging的jar包
然后就可以了
也可以用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
用BeanUtils和PropertyUtils都可以操作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>、期待与您交流! ---------------------