java反射詳解 (一)

 【案例1】經過一個對象得到完整的包名和類名java

  
  
  
  
  1. package Reflect; 
  2.   
  3. /** 
  4.  * 經過一個對象得到完整的包名和類名 
  5.  * */ 
  6. class Demo{ 
  7.     //other codes... 
  8.   
  9. class hello{ 
  10.     public static void main(String[] args) { 
  11.         Demo demo=new Demo(); 
  12.         System.out.println(demo.getClass().getName()); 
  13.     } 

 

【運行結果】:Reflect.Demo數組

添加一句:全部類的對象其實都是Class的實例。框架

【案例2】實例化Class類對象ide

 

  
  
  
  
  1. package Reflect; 
  2. class Demo{ 
  3.     //other codes... 
  4.   
  5. class hello{ 
  6.     public static void main(String[] args) { 
  7.         Class<?> demo1=null
  8.         Class<?> demo2=null
  9.         Class<?> demo3=null
  10.         try
  11.             //通常儘可能採用這種形式 
  12.             demo1=Class.forName("Reflect.Demo"); 
  13.         }catch(Exception e){ 
  14.             e.printStackTrace(); 
  15.         } 
  16.         demo2=new Demo().getClass(); 
  17.         demo3=Demo.class
  18.           
  19.         System.out.println("類名稱   "+demo1.getName()); 
  20.         System.out.println("類名稱   "+demo2.getName()); 
  21.         System.out.println("類名稱   "+demo3.getName()); 
  22.           
  23.     } 

 

  
  
  
  
  1. import java.lang.reflect.*; 
  2. class hello{ 
  3.     public static void main(String[] args) { 
  4.         int[] temp={1,2,3,4,5}; 
  5.         Class<?>demo=temp.getClass().getComponentType(); 
  6.         System.out.println("數組類型: "+demo.getName()); 
  7.         System.out.println("數組長度  "+Array.getLength(temp)); 
  8.         System.out.println("數組的第一個元素: "+Array.get(temp, 0)); 
  9.         Array.set(temp, 0100); 
  10.         System.out.println("修改以後數組第一個元素爲: "+Array.get(temp, 0)); 
  11.     } 

【案例3】經過Class實例化其餘類的對象函數

經過無參構造實例化對象this

  
  
  
  
  1. package Reflect; 
  2.   
  3. class Person{ 
  4.       
  5.     public String getName() { 
  6.         return name; 
  7.     } 
  8.     public void setName(String name) { 
  9.         this.name = name; 
  10.     } 
  11.     public int getAge() { 
  12.         return age; 
  13.     } 
  14.     public void setAge(int age) { 
  15.         this.age = age; 
  16.     } 
  17.     @Override 
  18.     public String toString(){ 
  19.         return "["+this.name+"  "+this.age+"]"
  20.     } 
  21.     private String name; 
  22.     private int age; 
  23.   
  24. class hello{ 
  25.     public static void main(String[] args) { 
  26.         Class<?> demo=null
  27.         try
  28.             demo=Class.forName("Reflect.Person"); 
  29.         }catch (Exception e) { 
  30.             e.printStackTrace(); 
  31.         } 
  32.         Person per=null
  33.         try { 
  34.             per=(Person)demo.newInstance(); 
  35.         } catch (InstantiationException e) { 
  36.             // TODO Auto-generated catch block 
  37.             e.printStackTrace(); 
  38.         } catch (IllegalAccessException e) { 
  39.             // TODO Auto-generated catch block 
  40.             e.printStackTrace(); 
  41.         } 
  42.         per.setName("Rollen"); 
  43.         per.setAge(20); 
  44.         System.out.println(per); 
  45.     } 

 

 

【運行結果】: spa

[Rollen  20]code

可是注意一下,當咱們把Person中的默認的無參構造函數取消的時候,好比本身定義只定義一個有參數的構造函數以後,會出現錯誤: orm

好比我定義了一個構造函數:對象

  
  
  
  
  1. public Person(String name, int age) { 
  2.         this.age=age; 
  3.         this.name=name; 
  4.     } 

 

 

而後繼續運行上面的程序,會出現:

java.lang.InstantiationException: Reflect.Person

    at java.lang.Class.newInstance0(Class.java:340)

    at java.lang.Class.newInstance(Class.java:308)

    at Reflect.hello.main(hello.java:39)

Exception in thread "main" java.lang.NullPointerException

    at Reflect.hello.main(hello.java:47)

因此你們之後再編寫使用Class實例化其餘類的對象的時候,必定要本身定義無參的構造函數

 

【案例】經過Class調用其餘類中的構造函數 (也能夠經過這種方式經過Class建立其餘類的對象)

  
  
  
  
  1. package Reflect; 
  2.   
  3. import java.lang.reflect.Constructor; 
  4.   
  5. class Person{ 
  6.       
  7.     public Person() { 
  8.           
  9.     } 
  10.     public Person(String name){ 
  11.         this.name=name; 
  12.     } 
  13.     public Person(int age){ 
  14.         this.age=age; 
  15.     } 
  16.     public Person(String name, int age) { 
  17.         this.age=age; 
  18.         this.name=name; 
  19.     } 
  20.     public String getName() { 
  21.         return name; 
  22.     } 
  23.     public int getAge() { 
  24.         return age; 
  25.     } 
  26.     @Override 
  27.     public String toString(){ 
  28.         return "["+this.name+"  "+this.age+"]"
  29.     } 
  30.     private String name; 
  31.     private int age; 
  32.   
  33. class hello{ 
  34.     public static void main(String[] args) { 
  35.         Class<?> demo=null
  36.         try
  37.             demo=Class.forName("Reflect.Person"); 
  38.         }catch (Exception e) { 
  39.             e.printStackTrace(); 
  40.         } 
  41.         Person per1=null
  42.         Person per2=null
  43.         Person per3=null
  44.         Person per4=null
  45.         //取得所有的構造函數 
  46.         Constructor<?> cons[]=demo.getConstructors(); 
  47.         try
  48.             per1=(Person)cons[0].newInstance(); 
  49.             per2=(Person)cons[1].newInstance("Rollen"); 
  50.             per3=(Person)cons[2].newInstance(20); 
  51.             per4=(Person)cons[3].newInstance("Rollen",20); 
  52.         }catch(Exception e){ 
  53.             e.printStackTrace(); 
  54.         } 
  55.         System.out.println(per1); 
  56.         System.out.println(per2); 
  57.         System.out.println(per3); 
  58.         System.out.println(per4); 
  59.     } 

 

 

【運行結果】:

[null  0]

[Rollen  0]

[null  20]

[Rollen  20]

【案例】 

返回一個類實現的接口:

  
  
  
  
  1. package Reflect; 
  2.   
  3. interface China{ 
  4.     public static final String name="Rollen"
  5.     public static  int age=20
  6.     public void sayChina(); 
  7.     public void sayHello(String name, int age); 
  8.   
  9. class Person implements China{ 
  10.     public Person() { 
  11.           
  12.     } 
  13.     public Person(String sex){ 
  14.         this.sex=sex; 
  15.     } 
  16.     public String getSex() { 
  17.         return sex; 
  18.     } 
  19.     public void setSex(String sex) { 
  20.         this.sex = sex; 
  21.     } 
  22.     @Override 
  23.     public void sayChina(){ 
  24.         System.out.println("hello ,china"); 
  25.     } 
  26.     @Override 
  27.     public void sayHello(String name, int age){ 
  28.         System.out.println(name+"  "+age); 
  29.     } 
  30.     private String sex; 
  31.   
  32. class hello{ 
  33.     public static void main(String[] args) { 
  34.         Class<?> demo=null
  35.         try
  36.             demo=Class.forName("Reflect.Person"); 
  37.         }catch (Exception e) { 
  38.             e.printStackTrace(); 
  39.         } 
  40.         //保存全部的接口 
  41.         Class<?> intes[]=demo.getInterfaces(); 
  42.         for (int i = 0; i < intes.length; i++) { 
  43.             System.out.println("實現的接口   "+intes[i].getName()); 
  44.         } 
  45.     } 

 

 

【運行結果】:

實現的接口   Reflect.China

(注意,如下幾個例子,都會用到這個例子的Person類,因此爲節省篇幅,此處再也不粘貼Person的代碼部分,只粘貼主類hello的代碼)

【案例】:取得其餘類中的父類

  
  
  
  
  1. class hello{ 
  2.     public static void main(String[] args) { 
  3.         Class<?> demo=null
  4.         try
  5.             demo=Class.forName("Reflect.Person"); 
  6.         }catch (Exception e) { 
  7.             e.printStackTrace(); 
  8.         } 
  9.         //取得父類 
  10.         Class<?> temp=demo.getSuperclass(); 
  11.         System.out.println("繼承的父類爲:   "+temp.getName()); 
  12.     } 

 

 

【運行結果】

繼承的父類爲:   java.lang.Object

【案例】:得到其餘類中的所有構造函數

這個例子須要在程序開頭添加import java.lang.reflect.*;

而後將主類編寫爲:

  
  
  
  
  1. class hello{ 
  2.     public static void main(String[] args) { 
  3.         Class<?> demo=null
  4.         try
  5.             demo=Class.forName("Reflect.Person"); 
  6.         }catch (Exception e) { 
  7.             e.printStackTrace(); 
  8.         } 
  9.         Constructor<?>cons[]=demo.getConstructors(); 
  10.         for (int i = 0; i < cons.length; i++) { 
  11.             System.out.println("構造方法:  "+cons[i]); 
  12.         } 
  13.     } 

 

 

【運行結果】:

構造方法:  public Reflect.Person()

構造方法:  public Reflect.Person(java.lang.String)

可是細心的讀者會發現,上面的構造函數沒有public 或者private這一類的修飾符

下面這個例子咱們就來獲取修飾符

  
  
  
  
  1. class hello{ 
  2.     public static void main(String[] args) { 
  3.         Class<?> demo=null
  4.         try
  5.             demo=Class.forName("Reflect.Person"); 
  6.         }catch (Exception e) { 
  7.             e.printStackTrace(); 
  8.         } 
  9.         Constructor<?>cons[]=demo.getConstructors(); 
  10.         for (int i = 0; i < cons.length; i++) { 
  11.             Class<?> p[]=cons[i].getParameterTypes(); 
  12.             System.out.print("構造方法:  "); 
  13.             int mo=cons[i].getModifiers(); 
  14.             System.out.print(Modifier.toString(mo)+" "); 
  15.             System.out.print(cons[i].getName()); 
  16.             System.out.print("("); 
  17.             for(int j=0;j<p.length;++j){ 
  18.                 System.out.print(p[j].getName()+" arg"+i); 
  19.                 if(j<p.length-1){ 
  20.                     System.out.print(","); 
  21.                 } 
  22.             } 
  23.             System.out.println("){}"); 
  24.         } 
  25.     } 

 

 

【運行結果】:

構造方法:  public Reflect.Person(){}

構造方法:  public Reflect.Person(java.lang.String arg1){}

有時候一個方法可能還有異常,呵呵。下面看看:

  
  
  
  
  1. class hello{ 
  2.     public static void main(String[] args) { 
  3.         Class<?> demo=null
  4.         try
  5.             demo=Class.forName("Reflect.Person"); 
  6.         }catch (Exception e) { 
  7.             e.printStackTrace(); 
  8.         } 
  9.         Method method[]=demo.getMethods(); 
  10.         for(int i=0;i<method.length;++i){ 
  11.             Class<?> returnType=method[i].getReturnType(); 
  12.             Class<?> para[]=method[i].getParameterTypes(); 
  13.             int temp=method[i].getModifiers(); 
  14.             System.out.print(Modifier.toString(temp)+" "); 
  15.             System.out.print(returnType.getName()+"  "); 
  16.             System.out.print(method[i].getName()+" "); 
  17.             System.out.print("("); 
  18.             for(int j=0;j<para.length;++j){ 
  19.                 System.out.print(para[j].getName()+" "+"arg"+j); 
  20.                 if(j<para.length-1){ 
  21.                     System.out.print(","); 
  22.                 } 
  23.             } 
  24.             Class<?> exce[]=method[i].getExceptionTypes(); 
  25.             if(exce.length>0){ 
  26.                 System.out.print(") throws "); 
  27.                 for(int k=0;k<exce.length;++k){ 
  28.                     System.out.print(exce[k].getName()+" "); 
  29.                     if(k<exce.length-1){ 
  30.                         System.out.print(","); 
  31.                     } 
  32.                 } 
  33.             }else
  34.                 System.out.print(")"); 
  35.             } 
  36.             System.out.println(); 
  37.         } 
  38.     } 

 

【運行結果】:

public java.lang.String  getSex ()

public void  setSex (java.lang.String arg0)

public void  sayChina ()

public void  sayHello (java.lang.String arg0,int arg1)

public final native void  wait (long arg0) throws java.lang.InterruptedException

public final void  wait () throws java.lang.InterruptedException

public final void  wait (long arg0,int arg1) throws java.lang.InterruptedException

public boolean  equals (java.lang.Object arg0)

public java.lang.String  toString ()

public native int  hashCode ()

public final native java.lang.Class  getClass ()

public final native void  notify ()

public final native void  notifyAll ()

【案例】接下來讓咱們取得其餘類的所有屬性吧,最後我講這些整理在一塊兒,也就是經過class取得一個類的所有框架

  
  
  
  
  1. class hello { 
  2.     public static void main(String[] args) { 
  3.         Class<?> demo = null
  4.         try { 
  5.             demo = Class.forName("Reflect.Person"); 
  6.         } catch (Exception e) { 
  7.             e.printStackTrace(); 
  8.         } 
  9.         System.out.println("===============本類屬性========================"); 
  10.         // 取得本類的所有屬性 
  11.         Field[] field = demo.getDeclaredFields(); 
  12.         for (int i = 0; i < field.length; i++) { 
  13.             // 權限修飾符 
  14.             int mo = field[i].getModifiers(); 
  15.             String priv = Modifier.toString(mo); 
  16.             // 屬性類型 
  17.             Class<?> type = field[i].getType(); 
  18.             System.out.println(priv + " " + type.getName() + " " 
  19.                     + field[i].getName() + ";"); 
  20.         } 
  21.         System.out.println("===============實現的接口或者父類的屬性========================"); 
  22.         // 取得實現的接口或者父類的屬性 
  23.         Field[] filed1 = demo.getFields(); 
  24.         for (int j = 0; j < filed1.length; j++) { 
  25.             // 權限修飾符 
  26.             int mo = filed1[j].getModifiers(); 
  27.             String priv = Modifier.toString(mo); 
  28.             // 屬性類型 
  29.             Class<?> type = filed1[j].getType(); 
  30.             System.out.println(priv + " " + type.getName() + " " 
  31.                     + filed1[j].getName() + ";"); 
  32.         } 
  33.     } 

 

 

【運行結果】:

===============本類屬性========================

private java.lang.String sex;

===============實現的接口或者父類的屬性========================

public static final java.lang.String name;

public static final int age;

【案例】其實還能夠經過反射調用其餘類中的方法:

  
  
  
  
  1. class hello { 
  2.     public static void main(String[] args) { 
  3.         Class<?> demo = null
  4.         try { 
  5.             demo = Class.forName("Reflect.Person"); 
  6.         } catch (Exception e) { 
  7.             e.printStackTrace(); 
  8.         } 
  9.         try
  10.             //調用Person類中的sayChina方法 
  11.             Method method=demo.getMethod("sayChina"); 
  12.             method.invoke(demo.newInstance()); 
  13.             //調用Person的sayHello方法 
  14.             method=demo.getMethod("sayHello", String.class,int.class); 
  15.             method.invoke(demo.newInstance(),"Rollen",20); 
  16.               
  17.         }catch (Exception e) { 
  18.             e.printStackTrace(); 
  19.         } 
  20.     } 

 

 

【運行結果】:

hello ,china

Rollen  20

【案例】調用其餘類的setget方法

  
  
  
  
  1. class hello { 
  2.     public static void main(String[] args) { 
  3.         Class<?> demo = null
  4.         Object obj=null
  5.         try { 
  6.             demo = Class.forName("Reflect.Person"); 
  7.         } catch (Exception e) { 
  8.             e.printStackTrace(); 
  9.         } 
  10.         try
  11.          obj=demo.newInstance(); 
  12.         }catch (Exception e) { 
  13.             e.printStackTrace(); 
  14.         } 
  15.         setter(obj,"Sex","男",String.class); 
  16.         getter(obj,"Sex"); 
  17.     } 
  18.   
  19.     /** 
  20.      * @param obj 
  21.      *            操做的對象 
  22.      * @param att 
  23.      *            操做的屬性 
  24.      * */ 
  25.     public static void getter(Object obj, String att) { 
  26.         try { 
  27.             Method method = obj.getClass().getMethod("get" + att); 
  28.             System.out.println(method.invoke(obj)); 
  29.         } catch (Exception e) { 
  30.             e.printStackTrace(); 
  31.         } 
  32.     } 
  33.   
  34.     /** 
  35.      * @param obj 
  36.      *            操做的對象 
  37.      * @param att 
  38.      *            操做的屬性 
  39.      * @param value 
  40.      *            設置的值 
  41.      * @param type 
  42.      *            參數的屬性 
  43.      * */ 
  44.     public static void setter(Object obj, String att, Object value, 
  45.             Class<?> type) { 
  46.         try { 
  47.             Method method = obj.getClass().getMethod("set" + att, type); 
  48.             method.invoke(obj, value); 
  49.         } catch (Exception e) { 
  50.             e.printStackTrace(); 
  51.         } 
  52.     } 
  53. }// end class 

 

【運行結果】:

相關文章
相關標籤/搜索