zoukankan      html  css  js  c++  java
  • Java基础巩固——反射

    什么是反射


       反射机制就是指程序运行时能够获取自身的信息。在Java中,只要给出类的名字,就可以通过反射机制来获取类的信息

    哪里用的到反射机制


       在jdbc中就是使用的反射来实例化对象,比如:Class.forName("com.mysql.jdbc.Driver.class").newInstance();

       框架都用到反射机制,spring,hibernate、struts都是用反射机制实现的。

    反射机制的优点和缺点


      为什么要用反射机制?直接创建对象不就可以了吗,这就涉及到了动态与静态的概念,
        静态编译:在编译时确定类型,绑定对象,即通过。
        动态编译:运行时确定类型,绑定对象。动态编译最大限度发挥了java的灵活性,体现了多
        态的应用,有以降低类之间的藕合性。
        一句话,反射机制的优点就是可以实现动态创建对象和编译,体现出很大的灵活性,特别是在J2EE的开发中.

      它的灵活性就表现的十分明显。比如,一个大型的软件,不可能一次就把把它设计的很完美,当这个程序编译后,发布了,当发现需要更新某些功能时,我们不可能要用户把以前的卸载,再重新安装新的版本,假如这样的话,这个软件肯定是没有多少人用的。采用静态的话,需要把整个程序重新编译一次才可以实现功能的更新,而采用反射机制的话,它就可以不用卸载,只需要在运行时才动态的创建和编译,就可以实现该功
        能。
           它的缺点是对性能有影响。使用反射基本上是一种解释操作,我们可以告诉JVM,我们希望做什么并且它
        满足我们的要求。这类操作总是慢于只直接执行相同的操作。

    利用反射机制能干什么


    Class c=Class.forName("className");注明:className必须为全名,也就是得包含包名,比如,cn.netjava.pojo.UserInfo;
    Object obj=c.newInstance();//创建对象的实例

    获取构造器

      Constructor getConstructor(Class[] params)//根据指定参数获得public构造器

      Constructor[] getConstructors()//获得public的所有构造器

      Constructor getDeclaredConstructor(Class[] params)//根据指定参数获得public和非public的构造器

      Constructor[] getDeclaredConstructors()//获得public的所有构造器

    获取类的方法

      Method getMethod(String name, Class[] params),根据方法名,参数类型获得方法

        Method[] getMethods()//获得所有的public方法

        Method getDeclaredMethod(String name, Class[] params)//根据方法名和参数类型,获得public和非public的方法

        Method[] getDeclaredMethods()//获得所以的public和非public方法

    获取类的属性

      Field getField(String name)//根据变量名得到相应的public变量

          Field[] getFields()//获得类中所以public的方法

          Field getDeclaredField(String name)//根据方法名获得public和非public变量

          Field[] getDeclaredFields()//获得类中所有的public和非public方法

    使用实例


     获取类属性:

    public class TestGetField extends Object {
        private static final long serialVersionUID = -2862585049955236662L;
    
        public static void main(String args[]) throws Exception {
            Class<?> clazz = Class.forName("reflect.TestGetField");
            System.out.println("===============本类属性===============");
            // 取得本类属性
            Field[] fields = clazz.getDeclaredFields();
            getField(fields);
    
    
            System.out.println("==========实现的接口或者父类的属性==========");
            // 取得实现的接口或者父类的属性
            Field[] fatherField = clazz.getFields();
            getField(fatherField);
        }
    
        public static void getField(Field[] fields) {
            for (Field field : fields) {
                // 权限修饰符
                int mo = field.getModifiers();
                String priv = Modifier.toString(mo);
                Class<?> type = field.getType();
                System.out.println(priv + " " + type.getName() + " " + field.getName() + ";");
            }
        }
    }

     获取类的方法:

    public class TestGetMethod implements Serializable{
        private static final String  testString= "hello";
        public static void main(String args[]) throws Exception{
            Class<?> clazz = Class.forName("reflect.TestGetMethod");
            Method[] methods = clazz.getMethods();
            for (Method method :methods){
                Class<?> returnType = method.getReturnType();
                Class<?> para[] = method.getParameterTypes();
                int temp = method.getModifiers();
                System.out.print(Modifier.toString(temp));
                System.out.print(returnType.getName());
                System.out.print(method.getName());
    
                for (Class par:para){
                    System.out.println(par.getName());
                }
                
            }
        }
    }

     实例化类:

    public class TestNewInstance {
        public static void main(String[] args) throws Exception{
            Class<?> class1 = null;
            class1 = Class.forName("reflect.User");
            // 第一种方法,实例化默认构造方法,调用set赋值
            User user = (User)class1.newInstance();
            user.setAge(20);
            user.setName("adf");
            System.out.println(user);
            // 第二种 取得全部的构造函数 使用构造函数赋值
    
        }
    }
    
    class User {
        private int age;
        private String name;
        public User() {
            super();
        }
        public User(String name) {
            super();
            this.name = name;
        }
        public User(int age, String name) {
            super();
            this.age = age;
            this.name = 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 "User [age=" + age + ", name=" + name + "]";
        }
    }

    使用类的方法:

    public class TestUseMethod {
        public static void main(String[] args)throws Exception{
            Class<?> clazz = Class.forName("reflect.TestUseMethod");
            // 调用reflect1方法
            Method method = clazz.getMethod("reflect1");
            method.invoke(clazz.newInstance());
    
            // 调用reflect2方法
            method = clazz.getMethod("reflect2", int.class, String.class);
            method.invoke(clazz.newInstance(),20,"test");
        }
    
        public void reflect1() {
            System.out.println("Java 反射机制 - 调用某个类的方法1.");
        }
        public void reflect2(int age, String name) {
            System.out.println("Java 反射机制 - 调用某个类的方法2.");
            System.out.println("age -> " + age + ". name -> " + name);
        }
    }

    动态代理:

    public class TestProxy {
        public static void main(String args[]) throws Exception{
            MyInvocationHandler demo = new MyInvocationHandler();
            Subject subject = (Subject) demo.bind(new RealSubject());
            System.out.println(subject.say("janti",20));
        }
    }
    
    interface Subject{
        public String say(String name, int age);
    }
    
    // 定义真实项目
    class RealSubject implements Subject {
        public String say(String name, int age) {
            return name + "  " + age;
        }
    }
    
    //如果想要完成动态代理,首先需要定义一个InvocationHandler接口的子类,已完成代理的具体操作。
    
    class MyInvocationHandler implements InvocationHandler{
    
        private Object object = null;
        public Object bind(Object obj){
            this.object = obj;
            return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),this);
        }
    
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            Object temp = method.invoke(this.object,args);
            return temp;
        }
    }
  • 相关阅读:
    程序员装逼指南(网上看到的,笑喷了……)
    【转】Flex4 中skinclass使用Path绘制多边形
    批处理根据星期自动备份到服务器的相应文件夹
    精妙Sql语句备忘
    The project file ' ' has been renamed or is no longer in the solution. 解决方案
    HttpPostedFileBase always null when upload file by asp.net mvc4 mobile
    解决.net绘制的 WinForm 在 windows7下变形的方法
    select语句中,直接为选出的数据添加uniqueidentifier字段
    向各位请教一个关于Decorator模式的问题
    子查询基础知识
  • 原文地址:https://www.cnblogs.com/superfj/p/8711261.html
Copyright © 2011-2022 走看看