---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------
JavaBean
什么是javaBean?
1,按照一定特殊规则编写的类,主要用于传递数据信息,这种类中的方法主要用于访问私有的字段,且方法名符合某种命名规则。
2,如果要再两个模块之间传递多个信息,可以将信息封装到JavaBean中,这种JavaBean的实例对象
通常称之为值对象(Value Object,简称VO)。这些信息在类中用私有字段来存储,如果读取
或设置字段的值,则需要通过一些相应的方法来访问,JavaBean的属性时根据其中的setter和getter方法来确定的,而不是
根据其中的成员变量。如果方法名为setId,中文意思即为设置id,
总之,JavaBean的属性根据方法名推断出来的,它根本看不到java类内部的成员变量
3,一个用于被外部调用的类,设置了私有的属性,并提供了get和set方法。
获取属性的规则:
a,去掉方法名中get或者set
b, 如果第二个字母是小写的,则把第一个字母变成小写的。
如:Age ----> age;CPU ----> CPU
4,一个符合JavaBean的好处:
a,在java EE开发中,经常要使用到JavaBean,很多环境要求按JavaBean方式进行操作。
b,JDK中提供了对JavaBean进行操作的一些API,这套API就称为内省(IntroSprctor)。用这套API操作JavaBean比普通类的方式更方便。
内省的综合案例:
1,用PropertyDescriptor对象可以获得某一个类中的属性,
及其getter和setter方法。
2,采用遍历BeanInfo的所有属性方法来查找和设置某个类对象的对应的属性,在程序中把一个类,
看作JavaBean,就是调用IntroSpector.getBeanInfo方法,得到BeanInfo对象,封装了把这个类当作JavaBean看的结果信息。
内省的应用示例:
package com.itheima.reflectdemos;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
/**
* 内省的简单操作示例
* @author wuyong
*
*/
public class IntroSpectorDemo {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
//实例化一个javaBean类对象
User user = new User("哈哈", 18);
String propertyName = "name";
//获得某一对象中的某一属性,并输出
Object retVal = getProperty(user, propertyName);
System.out.println("对象的名称是:" + retVal);
//设置对象的属性值。
String name = "宝宝";
setProperty(user, propertyName, name);
System.out.println("设置后的新名称是:" + user.getName());
}
/**
* 通过内省操作,设置对象的属性值
* @param obj
* @param propertyName
* @param pram
* @throws IntrospectionException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private static void setProperty(Object obj, String propertyName, Object pram)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
//获得指定对象中对应属性名称的属性
PropertyDescriptor pd2 = new PropertyDescriptor(propertyName, obj.getClass());
//获得对应属性的只写属性,并执行
Method setMethod = pd2.getWriteMethod();
setMethod.invoke(obj, pram);
}
/**
* 通过内省操作,读取对象中对应的属性名称的值。
* @param obj
* @param propertyName
* @return
* @throws IntrospectionException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private static Object getProperty(Object obj, String propertyName)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
//用于接收方法的返回值
Object retVal = null;
PropertyDescriptor pd = new PropertyDescriptor(propertyName,obj.getClass());
//获取该属性的只读方法即get方法
Method getMethod = pd.getReadMethod();
//执行获取到的方法,并输出
retVal = getMethod.invoke(obj, null);
/**
* 以下是比较繁琐的方式
*/
//通过Introspector内省类,获得BeanInfo的对象
BeanInfo bi = Introspector.getBeanInfo(obj.getClass());
//BeanInfo类的getPropertyDescriptors()方法获得所有的属性
PropertyDescriptor[] pds = bi.getPropertyDescriptors();
//遍历数组,判断数组中的属性名称是否与传入的参数名称一致,一致则获取其只读方法,并执行,结束循环。
for (int i = 0; i < pds.length; i++) {
if (pds[i].getName().equals(propertyName)) {
Method getMethod2 = pds[i].getReadMethod();
retVal = getMethod2.invoke(obj, null);
break;
}
}
return retVal;
}
}
class User {
//名称
private String name;
//年龄
private int age;
//构造函数
public User(){}
public User(String name,int age){
this.name = name;
this.age = age;
}
//getter和setter访问器
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
/**
* 重新hashCode方法
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
/**
* 重新equals方法
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public void setAge(int age) {
this.age = age;
}
}
---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------