Java反射常用方法

來源:互聯網
上載者:User

 

執行個體如下:

  1. package com;
  2. /***
  3.  * 
  4.  * 鳥類
  5.  * 
  6.  * @author Administrator
  7.  *
  8.  */
  9. public class Bird implements Animal {
  10.     public Bird() {
  11.     
  12.     }
  13.     public String color;
  14.     private int age;
  15.     public int getAge() {
  16.         return age;
  17.     }
  18.     public void setAge(int age) {
  19.         this.age = age;
  20.     }
  21.     public String getColor() {
  22.         return color;
  23.     }
  24.     public void setColor(String color) {
  25.         this.color = color;
  26.     }
  27.     
  28.     
  29. }

 

  1. package com;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.lang.reflect.Method;
  5. import java.lang.reflect.Modifier;
  6. public class Test {
  7.     /**
  8.      * @param args
  9.      * @throws ClassNotFoundException 
  10.      * @throws NoSuchFieldException 
  11.      * @throws SecurityException 
  12.      * @throws IllegalAccessException 
  13.      * @throws IllegalArgumentException 
  14.      * @throws NoSuchMethodException 
  15.      * @throws InvocationTargetException 
  16.      */
  17.     public static void main(String[] args) throws ClassNotFoundException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
  18.     
  19.         
  20.         // 測試Java反射機制
  21.         Class c = null;
  22.         // 建立類的執行個體
  23.         c = Class.forName("com.Bird");
  24.         System.out.println(c.getName());
  25.         
  26.         Package p = null;
  27.         p = c.getPackage();
  28.         System.out.println(p.getName());
  29.         // 返回一個 Field 對象,該對象反映此 Class 對象所表示的類或介面的指定已聲明欄位。
  30.         Field[] f = c.getDeclaredFields();
  31.         for(int i=0; i<f.length; i++) {
  32.             System.out.println(f[i].getType().getName());
  33.             System.out.println(Modifier.toString(f[i].getModifiers()));
  34.             System.out.println(f[i].getName());
  35.         }
  36.         
  37.         Class[] cc = c.getInterfaces();
  38.         for(Class cite: cc) {
  39.             System.out.println(cite.getName());
  40.         }
  41.         
  42.         c = Class.forName("com.Bird");
  43.         Field ff = c.getField("color");
  44.         Bird bird = new Bird();
  45.         System.out.println("bird color: " + bird.getColor());
  46.         ff.set(bird, "red");
  47.         System.out.println("bird color: " + bird.getColor());
  48.         
  49.         Method m = c.getMethod("setColor", Class.forName("java.lang.String"));
  50.         Bird bf = new Bird();
  51.         System.out.println("bf color: " + bf.getColor());
  52.         m.invoke(bf,"orange");
  53.         System.out.println("bf color: " + bf.getColor());
  54.         
  55.     }
  56. }

 

運行結果:

com.Bird
com
java.lang.String
public
color
int
private
age
com.Animal
bird color: null
bird color: red
bf color: null
bf color: orange

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.