1. 程式人生 > >Java反射研究(2)

Java反射研究(2)

接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;
	}
}