操作步驟: 1:實例化Class; 2:通過Class類的getMethod()方法取得一個Method的對象,並設置次方法操作時所需的參數類型; 3:使用invoke進行調用,並向方法中傳遞要設置的參數,但在使用invoke()方法時必須傳入一個類的實例化對象 實例1:sayChina()方法中沒有參數 package cn.itcast02; import java.lang.reflect.Method; //調用Person類中的sayChina()方法,沒有參數 public class ClassDemo04 { public static void main(String[] args) { Class<?> c1 = null; try { c1 = Class. forName("cn.itcast02.Person"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { //獲得一個method對象 Method method = c1.getMethod( "sayChina"); //調用方法,必須傳遞對象實例 method.invoke(c1.newInstance()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 實例2:sayHello()方法中有參數 package cn.itcast02; import java.lang.reflect.Method; //調用Person類中的sayHello()方法,有參數 public class ClassDemo05 { public static void main(String[] args) { Class<?> c1 = null; try { c1 = Class. forName("cn.itcast02.Person"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { Method method = c1.getMethod( "sayHello", String.class,int .class,char. class); //調用方法,必須傳遞對象實例,同時傳遞三個參數值 Object invoke = method.invoke(c1.newInstance(),"侯勇" ,50,'男' ); System. out.println(invoke); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
實例3:調用getter和setter方法 package cn.itcast02; import java.lang.reflect.Method; //調用Person類中的sayChina()方法 public class ClassDemo04 { public static void main(String[] args) { Class<?> c1 = null; Object obj = null; try { c1 = Class. forName("cn.itcast02.Person"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { obj = c1.newInstance(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { Method method = obj.getClass().getMethod("setName" ,String.class); Method method2 = obj.getClass().getMethod("setAge" ,int.class); method.invoke(obj, "小紅"); method2.invoke(obj, 30); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { Method method = obj.getClass().getMethod("getName" ); Method method2 = obj.getClass().getMethod("getAge" ); System. out.println(method.invoke(obj)); System. out.println(method2.invoke(obj)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Person類 package cn.itcast02; public class Person implements China{ private String name; private int age; private char sex; public Person() { super(); } public Person(String name, int age, char sex) { super(); this.name = name; this.age = age; this.sex = sex; } 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 char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public void eat() { System.out.println("吃了"); } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]"; } @Override public void sayChina() { // TODO Auto-generated method stub System.out.println("作者:"+ AUTHOR + "國籍:"+ NATIONAL); } @Override public String sayHello(String name, int age, char sex) { // TODO Auto-generated method stub return "姓名:" + name +"年齡:"+ age +"性別:"+ sex; } }
China接口 package cn.itcast02; public interface China { public static final String NATIONAL = "China"; public static final String AUTHOR = "成龍"; public void sayChina(); public String sayHello(String name, int age, char sex); }