黑馬程式設計師--java高新技術--javaBean內省
阿新 • • 發佈:2019-01-26
------- android培訓、java培訓、期待與您交流! ---------
內省(IntroSpector) 主要對JavaBean進行操作。
JavaBean:
特殊的java類。JavaBean的屬性是根據其中的get和set來確定的,
gettime-->time
setTime-->time
getCPU-->CPU
如果一個類符合JavaBean的特點,可以當做普通類處理,也可以當做JavaBean處理。
如果在兩個模組之間傳遞多個資訊,可以將這些資訊封裝到一個JavaBean中。這種JavaBean
的例項物件通常稱為值物件。
對JavaBean的簡單內省操作
通過內省的方式對Person物件中的成員變數進行讀寫操作。
並對生成的程式碼進行重構。
通過反射對某個屬性進行讀寫操作的程式碼可以抽取為通用方法,通過MyEclipse可以非常輕鬆實現這個功能。
步驟如下:選中需要重構的程式碼,右擊-->Refactor-->Extract Method...。
給抽取後的方法取一個名字,然後點選OK。
JavaBean:
書寫格式為:
1.必須有無參建構函式
2.屬性必須私有, 我們稱為欄位
3.提供標準的getter和setter
public class ReflectPoint { /** * @param args */ //無參構造方法 ReflectPoint(){ } //get\set方法 private int x; private int 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 String str1 = "ball"; public String str2 = "basketball"; public String str3 = "itheima"; public ReflectPoint(int x, int y) { super(); this.x = x; this.y = y; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + x; result = prime * result + y; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; ReflectPoint other = (ReflectPoint) obj; if (x != other.x) return false; if (y != other.y) return false; return true; } @Override public String toString() { // TODO Auto-generated method stub return str1 + ":" + str2 + ":" + str3; } }
內省:
複雜的內省:ReflectPoint pt1 = new ReflectPoint(3, 5); String propertyName = "x"; //反射方法獲取x :"x"-->"X"-->"getX"-->Method GetX--> //內省方式 PropertyDescriptor(屬性名, 類) PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());//屬性描述符 //獲取x的讀方法。 Method methodGetX = pd.getReadMethod(); //呼叫get方法 Object retVal1 = methodGetX.invoke(pt1); //獲取x的讀方法。 Method methodSetX = pd.getWriteMethod(); //呼叫Set方法 methodGetX.invoke(pt1,7); System.out.println(pt1.getX());//7
BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass());
//得到bean中的所有屬性
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
Object retVal =null;
//迭代
for (PropertyDescriptor pd : pds) {
if(pd.getName().equals(propertyName)){
Method methodGetX = pd.getReadMethod();
retVal = methodGetX.invoke(pt1);
break;
}
}
使用BeanUtiles工具包操作JavaBean1.下載工具包。
2.工程下建立lib資料夾,把工具包放入資料夾,右擊工程,buildPath,add to buildPath。
BeanUtiles內省操作:
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ReflectPoint pt1 = new ReflectPoint(3,5);
String propertyName = "x";
//"x"-->"X"-->"getX"-->MethodGetX-->
//得到getProperty
Object retVal = getProperty(pt1, propertyName);
System.out.println(retVal);
Object value = 7;
//設定getProperty
setProperties(pt1, propertyName, value);
System.out.println(BeanUtils.getProperty(pt1, "x").getClass().getName());
BeanUtils.setProperty(pt1, "x", "9");
System.out.println(pt1.getX());
/*
//java7的新特性
Map map = {name:"zxx",age:18};
BeanUtils.setProperty(map, "name", "lhm");
*/
BeanUtils.setProperty(pt1, "birthday.time", "111");
System.out.println(BeanUtils.getProperty(pt1, "birthday.time"));
PropertyUtils.setProperty(pt1, "x", 9);
System.out.println(PropertyUtils.getProperty(pt1, "x").getClass().getName());
}
PropertyUtils:
PropertyUtils.setProperty(pt1, "x", 9);
System.out.println(PropertyUtils.getProperty(pt1, "x").getClass().getName());
PropertyUtils類也可以操作物件的屬性,但是他不能對屬性進行自動型別轉換。