---------------------- <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>、期待與您交流! ----------------------