MainActivity如下:
[java]
package cn.testreflect;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import android.os.Bundle;
import android.app.Activity;
/**
* Demo描述:
* 在一個類中有另外一個類的對象
* 比如此處的Worker類中有一個Student類的對象:
* private Student mStudent;
*
* 目前需求:
* 我們需要反射出Worker類中的 private Student mStudent
* 並且使用該對象調用其對應的方法
*/
public class MainActivity extends Activity {
private Field mStudentField;
private Object mStudentObject;
private Class mStudentClass;
private Method mStudentShowMethod;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
try {
String workerClassName="cn.testreflect.Worker";
Class workerClass=Class.forName(workerClassName);
//得到Worker類中的mStudent字段
mStudentField=workerClass.getDeclaredField("mStudent");
mStudentField.setAccessible(true);
//實例化mStudent對象
mStudentObject=mStudentField.get(new Worker());
//得到mStudent對象對應類的Class
mStudentClass = Class.forName(mStudentObject.getClass().getName());
//反射出該Class類中的show()方法
mStudentShowMethod = mStudentClass.getDeclaredMethod("show");
//取消訪問私有方法的合法性檢查
mStudentShowMethod.setAccessible(true);
//調用show()方法
mStudentShowMethod.invoke(mStudentObject);
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
Worker如下:
[java]
package cn.testreflect;
public class Worker {
private int age;
private String name;
private Student mStudent;
public Worker() {
super();
mStudent=new Student("I am a student", 9527);
System.out.println("---> public Worker(){ }");
}
public Worker(int age, String name) {
super();
this.age = age;
this.name = name;
mStudent=new Student("I am a student", 9527);
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);
}
}
Student如下:
[java]
package cn.testreflect;
public class Student {
private String name;
private int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = 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;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
public void show(){
System.out.println("-----> call method show()");
}
}
main.xml如下:
[html]
<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>