反射机制

来源:互联网 发布:泰格软件使用教程 编辑:程序博客网 时间:2024/06/03 08:37

Java反射机制是在运行状态中,对于任意一个类(class文件),都能够知道这个类的所有属性的方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为Java语言的反射机制。可以理解为对类的解剖。

获取字节码对象的方式:  1object类中的getclass方法想要用这种方式,必须要明确具体的类,并创建对象-----较为麻烦    Person p=new Person();    Class clazz=p.getClass();    Person p1=new Person();    Class clazz1=p1.getClass();    System.out.println(clazz==clazz1);  2、任何数据类型都具备静态的属性.class来获取其对应的class对象,相对简单,但是依然要明确用到类中的静态成员----还是不够扩展    Class clazz=Person.class;    Class clazz1=Person.class;    System.out.println(clazz==clazz1);   3、通过给定的类的字符串名称就可以获取该类,更为扩展可以用class类中的方法完成;该方法就是forName;这种方式只要有名称即可,更为方便,扩展性更强    String className="com.api.video.reflect.Person";    Class clazz=Class.forName(className);    System.out.println(clazz);

获取class文件中的构造函数以及方法

/** * 获取class中的构造函数 */import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;public class ReflectDemo1 {    public static void main(String[] args) throws Exception {        creatNewObject();        creatNewObject_2();        getFiledDemo();        getMethodDemo();        getMethodDemo_2();    }    /**     * 根据无参构造方法创建对象     * @throws Exception     */    public static void creatNewObject() throws Exception{        /*早期的时候先根据被new的类的名称寻找该类的字节码文件,并加载进内存,        并创建改字节码文件对象,并接着创建改字节文件的对应的person对象        Person p=new Person();*/        String name="com.api.video.reflect.Person";        //找寻该名称的类文件,加载进内存,并产生class对象        Class clazz=Class.forName(name);        //产生该类的对象:newInstance()   创建类的一个新实例        //即调用无参构造函数        Object obj=clazz.newInstance();    }    /**     * 根据有参构造方法创建对象     * @throws Exception     */    public static void creatNewObject_2() throws Exception {        //Person p=new Person(3,"小强");        //当要获取指定名称对应类中的所体现的对象时,而该对象初始化使用有参构造时        //既然是通过制定的构造函数进行对象的初始化,所以应该先获取到该构造函数。        //通过字节码文件对象即可完成        String name="com.api.video.reflect.Person";        //找寻该名称的类文件,加载进内存,并产生class对象        Class clazz=Class.forName(name);        //获取指定的构造函数对象        Constructor constructor=clazz.getConstructor(int.class,String.class);        Object obj=constructor.newInstance(39,"小明");    }    /**     * 获取字节码文件中的字段,即属性     * @throws Exception     */    public static void getFiledDemo() throws Exception{        Class clazz=Class.forName("com.api.video.reflect.Person");        //Field field = clazz.getField("age");//只能获取公有的,也可以获取父类中        //System.out.println(field);        Field field=clazz.getDeclaredField("name");//只获取本类,但包含私有        //对私有字段的访问取消权限检查:暴力访问        field.setAccessible(true);        Object obj=clazz.newInstance();        field.set(obj,"大放");        Object o=field.get(obj);        System.out.println(field);        System.out.println(o);    }    /**     * 获取class中的公共函数     * @throws Exception     */    public static void getMethodDemo() throws Exception{        Class clazz=Class.forName("com.api.video.reflect.Person");        Method[] methods=clazz.getMethods();        //获取的都是公有的方法,包含父类的方法        for(Method method:methods){            System.out.println(method);        }        //只获取本类的方法,包含私有方法        Method[] m=clazz.getDeclaredMethods();        for(Method method:m){            System.out.println(method);        }    }    /**     * 调用class中的方法     * @throws Exception     */    public static void getMethodDemo_2() throws Exception {        Class clazz=Class.forName("com.api.video.reflect.Person");        Method method=clazz.getMethod("show", null);//获取空参数一般方法        //Object obj=clazz.newInstance();        Constructor constructor=clazz.getConstructor(int.class,String.class);        Object obj=constructor.newInstance(36,"新建");        method.invoke(obj, null);        Method m=clazz.getMethod("paramentMethod",String.class,int.class);        Object obj1=clazz.newInstance();        m.invoke(obj1,"爱的",23);    }}
0 0
原创粉丝点击