Java反射机制
发布日期:2021-09-08 22:54:58 浏览次数:17 分类:技术文章

本文共 2718 字,大约阅读时间需要 9 分钟。

[X]Java反射机制允许运行中的Java程序对自身进行检查,并能直接操作程序的内部属性或方法,可动态生成类实例、变更属性内部以及调用方法。
public class Reflection{
public void methA(){}
public void methodB(){}
}
以下代码完成对Reflection类中,方法的直接调用和反射调用
protected void onCreate(Bundle savedInstanceState){
...
//直接调用
Reflection relfection =new Reflection();
reflection.methodA();
reflection.methodB();
...
//反射调用
try{
Class reflectionCls=Class.forName("com.example.reflection.Reflection");
Constructor cons=reflectionCls.getConstructor(new Class[]{});//无参数构造方法
Reflection reflectionIns=(Reflection)cons.newInstance(new Object[]{});
Method method=reflectionCls.getDeclaredMethod("methodA",new Class[]{});
method.invoke(reflectionIns, new Object[]{});//调用方法,方法无参数
java.lang.Class提供的获取类组件--“constructors、fields、methods”
* Constructor--指定的参数是class,java中一切都是对象
*
* Constructor getConstructor(Class[] params);//由指定的参数获取public构造方法
* Constructor[] getConstructor();//获取所有的public构造方法
* Constructor getDeclaredConstructor(Class[] params);//获取由参数指定的所有构造方法,不分access level
* Constructor[] getDeclaredConstructors();//获取所有的构造方法,不分access level
返回的是java.lang.reflect.Constructor对象,该类有一个newInstance的方法,可以接受一个object数组作为其唯一参数,返回该类的实例。
Class[] types=new Class[] { String.class, String.class};
Constructor cons=TwoString.class.getConstructor(types);//
Object[] args=new Object[]{"a","b"};
TwoString ts=(TwoString)cons.newInstance(args);
Object newInstance();//使用默认构造器构造一个实例
* Field
*
* Field getField(String name);//public field,包括继承来的
* Field getFields();// all public fields
* Field getDeclaredField(String name);//field
* Field[] getDeclaredFields();//all field
返回的是java.lang.reflect.Field实例,对所有基本数据类型提供getXXX、setXXX方法
public int incrementField(String name, Object obj)throws ...{
Field field=obj.getClass().getDeclaredField(name);//对传入的对象obj,getClass获取Class的信息
int value=field.getInt(obj)+1;
field.setInt(obj, value);
return value;
}
* Method
*
* Method getMethod(String name, Class[] params);//public method
* Method[] getMethods();//all public methods
* Method getDeclaredMethod(String name, Class[] params);//method
* Method[] getDeclaredMethods();//all methods
返回的是java.lang.reflect.Method实例,该Method有一个invoke方法,有两个参数,第一个参数指定类的实例,第二个参数指定该Method的参数数组。
public int incrementProperty(String name, Object obj){
String prop=Character.toUpperCase(name.charAt(0))+name.substring(1);
String name="get"+prop;
Class[] types= new Class[]{};
Method method=obj.getClass().getMethod(name,types);//无参方法
Object result=method.invoke(obj,new Object[0]);
int value =((Integer)result).initValue()+1;
name="set"+prop;
types=new Class[]{int.class};
method=obj.getClass().getMethod(name,types);//有参,参数类型为int
method.invoke(obj, new Object[]{new Integer(value)));
return value;
}

转载于:https://www.cnblogs.com/littlefishxu/p/3969207.html

转载地址:https://blog.csdn.net/weixin_34341229/article/details/93367294 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:模板:素数筛
下一篇:win7 访问不了网络共享问题

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年03月22日 14时20分37秒