Android反射出一个类中的其他类对象并调用其对应方法

MainActivity如下:

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如下:

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如下:

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如下:

<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>
上一篇:Nico Game Studio 2.设置页面读写 纹理载入与选择


下一篇:【python之路19】文件操作