標籤:
import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import android.os.Bundle; import android.app.Activity; /** * Demo描述: * Android中Java反射技術的使用樣本 * 在Java中描述位元組碼檔案(xxx.class)的類叫Class * 反射的過程可視為剖析Class的過程 */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ try { /** * 1 反射出無參的構造方法並得到對象 * 注意: * 1 在Class.forName()中應該傳入含有包名的類全名 * 2 newInstance()方法的本質是調用類的無參Public構造方法 */ String className1="cn.testreflect.Worker"; Class clazz1=Class.forName(className1); Object object1=clazz1.newInstance(); System.out.println("object1.toString()="+object1.toString()); /** * 2 反射出帶參數的構造方法並得到對象 */ String className2="cn.testreflect.Worker"; Class clazz2=Class.forName(className2); Constructor constructor1=clazz2.getConstructor(int.class,String.class); Object object2=constructor1.newInstance(18,"小明"); System.out.println("object2.toString()="+object2.toString()); /** * 3 擷取類的私人欄位 * 注意: * 擷取共有欄位應調用clazz3.getField(name)方法 */ String className3="cn.testreflect.Worker"; Class clazz3=Class.forName(className3); Field ageField1=clazz3.getDeclaredField("age"); System.out.println("ageField1="+ageField1); /** * 4 擷取和更改某個對象的私人欄位 * 即類比get()和set()方法 */ String className4="cn.testreflect.Worker"; Class clazz4=Class.forName(className4); Field ageField2=clazz4.getDeclaredField("age"); Object object4=constructor1.newInstance(18,"小明"); //取消訪問私人欄位的合法性檢查 ageField2.setAccessible(true); //擷取對象的私人欄位 Object ageObject4=ageField2.get(object4); System.out.println("ageObject4="+ageObject4); //再更改對象的私人欄位的值 ageField2.set(object4, 9527); //重新獲得 Object ageObject5=ageField2.get(object4); System.out.println("ageObject5="+ageObject5); /** * 5 調用對象的帶參數的方法 */ String className5="cn.testreflect.Worker"; Class clazz5=Class.forName(className5); Method method=clazz5.getMethod("printMessage", String.class,int.class,int.class); Object object5=clazz5.newInstance(); method.invoke(object5, "周星星",50,9527); } catch (Exception e) { System.out.println(e.toString()); } }
public class Worker { private int age; private String name; public Worker() { super(); System.out.println("---> public Worker(){ }"); } public Worker(int age, String name) { super(); this.age = age; this.name = name; System.out.println("---> public Worker(int age, String name){ }"); } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Worker [age=" + age + ", name=" + name + "]"; } public void printMessage(String name,int age,int salary){ System.out.println("name="+name+",age="+age+",salary="+salary); } }
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Android中Java反射技術的使用" android:layout_centerInParent="true"/> </RelativeLayout>
Android中Java反射技術的使用樣本