Java反射之Method調用和Filed設定__Java

來源:互聯網
上載者:User

反射是java的一個進階特性。

Reflection 是Java被視為動態(或准動態)語言的一個關鍵性質。這個機制允許程式在運行時透過Reflection APIs取得任何一個已知名稱的class的內部資訊,包括其modifiers(諸如public, static 等等)、superclass(例如Object)、實現之interfaces(例如Serializable),也包括fields和methods 的所有資訊,並可於運行時改變fields內容或調用methods。

反射就是給應用程式一個可以檢查自己和運行環境的一個路徑。自己資訊的替代—MetaData.

 

入門層級先看看如果通過反射調用方法和設定成員變數的值:

1.通過反射設定變數值

 

import java.lang.reflect.Field; /** * 反射修改Field * * @author yblin */ public class RefField { public String color; public Double number; public static void main(String args[]) { try { Class c = RefField.class; Field colorField; colorField = c.getField("color"); Field numberField = c.getField("number"); RefField obj = new RefField(); colorField.set(obj, "blue"); System.out.println("color=" + colorField.get(obj)); numberField.set(obj, 2.1); System.out.println("number=" + numberField.get(obj)); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

 

2.調用反射調用方法

import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * 反射方法調用。 * * @author yblin */ public class RefMethod { private String color; public void printColor() { System.out.println("color is:" + color); } public RefMethod(String color) { this.color = color; } public void sayHello(String name, Integer age) { System.out.println("Hello world," + name + "!" + "The age is:" + age); } public static void main(String args[]) { Class cls = RefMethod.class; RefMethod ref = new RefMethod("blue"); try { Method sayHello = cls .getMethod("sayHello", new Class[] { String.class, Integer.class });//尋找具有String參數和Integer參數的sayHello方法。 Method printColor = cls.getMethod("printColor", null); try { sayHello.invoke(ref, new Object[] { "ant", 3 });//通過Mechod對象的invoke調用來調用方法 printColor.invoke(ref, null); } catch (IllegalArgumentException e) {//無效參數 // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) {//無權訪問 // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.