Java 反射应用
一、通过反射获取对象的方式
二、获取默认的构造函数,实例化对象
三、获取其他构造函数,实例化对象
四、获取、设置 字段
五、调用方法
1.通过反射获取对象的方式
public void getObjectTest() throws ClassNotFoundException {
//反射获取对象的方法:
Class cls=null;
//方式一、通过对象方法. Object 类中的getClass()
StudentInfo s=new StudentInfo("王五",11);
cls=s.getClass();
//方式二、.通过静态成员 Class classzz=Person.class;
cls=StudentInfo.class;
// 3.Class.forName通过类的字符串名称获取. 包名+类名
String className="com.example.test3.reflex.StudentInfo";
//方式三:获取字节码对象
cls= Class.forName(className);
}
2.获取默认的构造函数,实例化对象
public void newInstanceTest() throws IllegalAccessException, InstantiationException, ClassNotFoundException {
String className="com.example.test3.reflex.StudentInfo";
Class cls= Class.forName(className); //获取字节码对象
Object obj= cls.newInstance(); //实例化对象,默认是无参的构造函数
}
3.获取其他构造函数,实例化对象
public void newInstance2Test() throws Exception {
String className="com.example.test3.reflex.StudentInfo";
Class cls= Class.forName(className); //获取字节码对象
Constructor constructor=cls.getConstructor(String.class,int.class);//第三步:获取公共的构造函数:
Object obj=constructor.newInstance("张山",12); //实例化对象
}
4.获取、设置 字段
public void fieldTest() throws Exception {
String className="com.example.test3.reflex.StudentInfo";
Class cls= Class.forName(className); //获取字节码对象
Constructor constructor=cls.getConstructor(String.class,int.class);//第三步:获取公共的构造函数:
Object obj=constructor.newInstance("张山",12); //实例化对象
//获取字段
//---------公共字段---------------
Field field1=cls.getField("name");//自能获取共有字段
String name=(String) field1.get(obj);
Log.e(TAG,"name1="+name);
field1.set(obj,"王五");
name=(String) field1.get(obj);
Log.e(TAG,"name2="+name);
//-------------------------------------
//----------私有字段---------------
Field fileld2=cls.getDeclaredField("age");//只能回去本类字段,但包含私有
fileld2.setAccessible(true); //对私有字段的访问取消权限检测,暴力访问
//字段只能通过对象去调用,所以需要传递对象
int age=fileld2.getInt(obj);//获取值
Log.e(TAG,"age1="+age);
fileld2.setInt(obj,11);//设置值
age=fileld2.getInt(obj);//获取值
Log.e(TAG,"age2="+age);
//-----------------------------
//----------静态字段------------
Field field3=cls.getField("TAG");//自能获取共有字段
String strV=(String) field3.get(null);
Log.e(TAG,"静态字段 TAG="+strV);
}
5.调用方法
public void methodTest() throws Exception{
String className="com.example.test3.reflex.StudentInfo";
Class cls= Class.forName(className); //获取字节码对象
Constructor constructor=cls.getConstructor(String.class,int.class);//第三步:获取公共的构造函数:
Object obj=constructor.newInstance("张山",12); //实例化对象
//-------------公共函数(只能调用公共函数)-----------
Method method= cls.getMethod("setName",String.class);
method.invoke(obj,"王二");//调用方法
//-------------公共函数--------------------------
//-------------私有函数(只能那本类方法,可以访问私有方法)---------
Method method2= cls.getDeclaredMethod("privMothod");//
method2.setAccessible(true);
method2.invoke(obj);//调用方法
//-------------私有函数--------------------------
//-------------静态函数--------------------------
Method method3 = cls.getMethod("staticMothod");
method3.invoke(null);
//-----------------------------------------------
}
测试类
package com.example.test3.reflex;
import android.util.Log;
public class StudentInfo {
public static String TAG="StudentInfo";
public String name;
private int age;
private StudentInfo(){
Log.e(TAG,"StudentInfo()");
}
public StudentInfo(String name,int age){
Log.e(TAG,"StudentInfo("+name+","+age+")");
this.name=name;
this.age=age;
}
public String getName(){
Log.e(TAG,"--getName--name="+name);
return name;
}
public int getAge(){
Log.e(TAG,"--getName--age="+age);
return age;
}
public void setName(String name){
Log.e(TAG,"setName name="+name);
this.name=name;
}
public void setAge(int age){
Log.e(TAG,"setAge age="+age);
this.age=age;
}
private void privMothod(){
Log.e(TAG,"访问私有方法privMothod");
}
public static void staticMothod(){
Log.e(TAG,"访问静态方法staticMothod");
}
}