Java反射的实例

来源:互联网 发布:mac下载windows系统 编辑:程序博客网 时间:2024/05/16 06:11
    JAVA反射机制是在运行状态中,对于任意一个类,都能够得到这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法;    
    这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制.
    概括一下:
    反射就是让你可以通过名称来得到对象(类,属性,方法)的技术。
    例如我们可以通过类名来生成一个类的实例;
    知道了方法名,就可以调用这个方法;知道了属性名就可以访问这个属性的值。
    
    1、获取类对应的Class对象
    运用(已知对象)getClass():Object类中的方法,每个类都拥有此方法。
    如:
    String str = new String();    Class strClass = str.getClass();
    运用(已知子类的class) Class.getSuperclass():Class类中的方法,返回该Class的父类的Class;
    运用(已知类全名):Class.forName() 静态方法运用(已知类): 类名.class
    
    2、通过类名来构造一个类的实例
    a、调用无参的构造函数:
    Class newoneClass = Class.forName(类全名);     newoneClass.newInstance();
    b、调用有参的构造函数:我们可以自定义一个函数。
 public Object newInstance(String className, Object[] args) throws Exception {//args为参数数组Class newoneClass = Class.forName(className); //得到参数的Class数组(每个参数的class组成的数组),由此来决定调用那个构造函数Class[] argsClass = new Class[args.length]; for (int i = 0, j = args.length; i < j; i++) {      argsClass[i] = args[i].getClass();}Constructor cons = newoneClass.getConstructor(argsClass); //根据argsClass选择函数return cons.newInstance(args); //根据具体参数实例化对象。 }

    3、得到某个对象的属性
    a、非静态属性:
    首先得到class,然后得到这个class具有的field,然后以具体实例为参数调用这个field

 public Object getProperty(Object owner, String fieldName) throws Exception { Class ownerClass = owner.getClass();//首先得到class Field field = ownerClass.getField(fieldName); //然后得到这个class具有的field,也可以通过getFields()得到所有的field Object property = field.get(owner); //owner指出了取得那个实例的这个属性值,如果这个属性是非公有的,这里会报IllegalAccessExceptionreturn property; }
    b、静态属性:
    只有最后一步不同,由于静态属性属于这个类,所以只要在这个类上调用这个field即可   
   Object property = field.get(ownerClass);
    4、执行某对象的方法
 public Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception { Class ownerClass = owner.getClass(); //也是从class开始的//得到参数的class数组,相当于得到参数列表的类型数组,来取决我们选择哪个函数。Class[] argsClass = new Class[args.length]; for (int i = 0, j = args.length; i < j; i++) {      argsClass[i] = args[i].getClass();}//根据函数名和函数类型来选择函数Method method = ownerClass.getMethod(methodName, argsClass); return method.invoke(owner, args);//具体实例下,具体参数值下调用此函数 }
    5、执行类的静态方法
    和上面的相似只是最后一行不需要指定具体实例
    return method.invoke(null, args);
    6、判断是否为某个类的实例
  public boolean isInstance(Object obj, Class cls) {    return cls.isInstance(obj);  }
    测试bean类:SimpleBean.java
    Java代码
    package com.royzhou.bean;    public class SimpleBean {        private String name;        private String[] hobby;        public SimpleBean() {        }        public SimpleBean(String name, String[] hobby) {                 this.name = name;                         this.hobby = hobby;        }        public void setName(String name) {            this.name = name;        }        public String getName() {            return this.name;        }        public void setHobby(String[] hobby) {            this.hobby = hobby;        }        public String[] getHobby() {            return this.hobby;        }        public String toString() {            String returnValue = super.toString() + "\n"; returnValue += "name:=" + this.name + "\n";                     if(this.hobby != null) {                returnValue += "hobby:";                for(String s : this.hobby) {                                     returnValue += s + ",";                }                returnValue += "\n";            }            return returnValue;        }    }
    反射测试类:ReflectTest.java
    Java代码
    package com.royzhou.bean;    import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method;    public class ReflectTest {        public static void main(String[] args) throws Exception {            Class clazz = SimpleBean.class;            //使用无参构造函数实例化bean            SimpleBean sb = (SimpleBean)clazz.newInstance();                     System.out.println(sb);            //使用有参构造函数实例化bean            Constructor constructor = clazz.getConstructor(new Class[]{String.class, String[].class});                     sb = (SimpleBean)constructor.newInstance(new Object[]{"royzhou",new String[]{"football","basketball"}});                         System.out.println(sb);            //为name字段设置值            Field field = clazz.getDeclaredField("name");                     field.setAccessible(true); //避免private不可访问抛出异常                        field.set(sb, "royzhou1985");                         System.out.println("modify name using Field:=" + sb.getName() + "\n");            //列出类SimpleBean的所有方法            Method[] methods = clazz.getDeclaredMethods();                         System.out.println("get methods of class SimpleBean:");            for(Method method : methods) {                if("setHobby".equals(method.getName())) {                    //动态调用类的方法来为hobby设置值                    method.invoke(sb, new Object[]{new String[]{"tennis","fishing"}});                }                System.out.println(method.getName());            }            System.out.println("\nset by invoke Method"); System.out.println(sb);        }    }
    输出结果:
    com.royzhou.bean.SimpleBean@757aef    name:=null    com.royzhou.bean.SimpleBean@d9f9c3    name:=royzhou    hobby:football,basketball,    modify name using Field:=royzhou1985    get methods of class SimpleBean:    setHobby    getHobby    getName    toString    setName    set by invoke Method    com.royzhou.bean.SimpleBean@d9f9c3    name:=royzhou1985    hobby:tennis,fishing,

0 0