Java反射研究(2)

來源:互聯網
上載者:User
接Java反射研究(1)九、調用特定方法

Method m = c1.getMethod("funcname",Class<?>...c);   //funcname表示調用方法的名稱,c表示參數的Class對象

例如:Method m = c1.getMethod("fun",String.class,int.class);    表示調用fun(String,int);函數

Object obj = m.invoke(c1.newInstance(),"xiazdong",20);   //如果有傳回值,則invoke函數返回;

註:如果是調用靜態方法,則不需要設定對象;

Object obj = m.invoke(null,"xiazdong");

註:如果參數中有數組,比如 public static void main(String[]args);

Method m = c1.getMethod("main",String[].class);

m.invoke(null,(Object)new String[]{"aa","bb"});是對的;

m.invoke(null,new String[]{"aa","bb"}); 會調用 main(String,String);函數;

十、調用特定屬性

Field f = c1.getDeclaredField("name");    //返回name屬性

f.setAccessible(true);    //私人屬性可見

String name = (String)f.get(Object obj);   //返回obj對象的name屬性的值

f.set(Object obj,String n);      //設定obj對象的name屬性為n值;

十一、運算元組

int tmp[] = {1,2,3};

Class<?> c  = tmp.getClass().getComponentType();

Array.getLength(tmp);        //tmp數組的長度

c.getName();           //獲得數群組類型名稱

Array.get(Object obj,int index);      //獲得obj數組的index索引的數值

Array.set(Object obj,int index,VALUE);    //設定obj數組的index索引的數值為value;

Object obj  = Array.newInstance(c,length);          //c為數組的類型,length為數組的長度;obj為返回的數組對象;

樣本:

import java.lang.reflect.*;public class GetMethodDemo01{public static void main(String args[])throws Exception{Class<?> c1 = Class.forName("Person");Method m = c1.getMethod("sayHello");m.invoke(c1.newInstance());Method m2 = c1.getMethod("sayHello2",String.class,int.class);String str = (String)m2.invoke(c1.newInstance(),"xiazdong",123);System.out.println(str);Field nameField = c1.getDeclaredField("name");Field ageField = c1.getDeclaredField("age");nameField.setAccessible(true);ageField.setAccessible(true);Person obj = (Person)c1.newInstance();obj.setName("xzdong");obj.setAge(12);System.out.println(nameField.get(obj));System.out.println(ageField.get(obj));Method setName = c1.getMethod("setName",String.class);setName.invoke(obj,"changed");Method getName = c1.getMethod("getName");System.out.println(getName.invoke(obj));int tmp[] = {1,2,3};Class<?> c3 = tmp.getClass().getComponentType();System.out.println(c3.getName());System.out.println("第一個數:"+Array.get(tmp,0));Array.set(tmp,0,5);System.out.println("第一個數:"+Array.get(tmp,0));Object arr = Array.newInstance(c3,5);System.arraycopy(tmp,0,arr,0,Array.getLength(tmp));System.out.println(Array.get(arr,2));System.out.println(Array.get(arr,3));}}

import java.lang.reflect.*;interface China{public static final String NAME = "CHINA";public int AGE = 60;public void sayHello();public String sayHello2(String name,int age);}class Person implements China{private String name;private int age;public String getName(){return name;}public void setName(String name){this.name = name;}public int getAge(){return age;}public void setAge(int age){this.age = age;}public void sayHello(){System.out.println(NAME+"  "+AGE);}public String sayHello2(String name,int age){return name+" "+age;}}

聯繫我們

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