zoukankan      html  css  js  c++  java
  • 反射

    获取class对象有三种方式
    1、已经有对象的情况下,调用对象的getClass方法
    2、不知道对象的情况下,只有对象的全限名字符
    3、直接类型.class

    
        public static  void  main(String []args)
                throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException,
                InstantiationException, ClassNotFoundException {
    
    
    
         /*
         * ------------------第一种方法--------------------------------
         * */
    
            User user=new User();
            //根据对象获取对应的class对象
            Class<? extends User> userClasszz = user.getClass();
    
            //获取所有public修改的字段
            Field[] fields = userClasszz.getFields();
            System.out.println(fields.length); //1
            //获取所有字段,包括私有的
            Field[] declaredFields = userClasszz.getDeclaredFields();
            System.out.println(declaredFields.length);//3
    
            //获取所有public方法,不包括私有的
            Method[] methods = userClasszz.getMethods();
            System.out.println(methods.length); //16 User里面七个public修饰的 Object里面9个
    
            //获取所有方法,包括私有的
            Method[] declaredMethods = userClasszz.getDeclaredMethods();
    
            System.out.println("declaredMethods--"+declaredMethods.length); //8个 不包括object
    
            //调用方法
            Method sayHello = userClasszz.getDeclaredMethod("sayHello");
    
             sayHello.invoke(user);
    
             Method sayHelloagrs=userClasszz.getDeclaredMethod("sayHello",String.class);
            sayHelloagrs.setAccessible(true);//设置是否进行安全检查,值为true,表示反射的对象在使用时应该取消java语言的访问检查,否则则表示实施访问检查
             sayHelloagrs.invoke(user,"张华");
    
    
            /*
             * ------------------第二种方法--------------------------------
             * */
    
            //使用全限名访问
    
            Class<?> aClass = null;
            User foruser=null;
    
            try {
                aClass = Class.forName("com.java.javaClass.common.User");
                foruser = (User)aClass.newInstance(); //创建user实例
            } catch (Exception e) {
                e.printStackTrace();
            }
            Method[] formethods = aClass.getMethods();
            System.out.println("---formethods.length:"+formethods.length);//16个包括object
    
            Method[] fordeclaredMethods = aClass.getDeclaredMethods();
            System.out.println("fordeclaredMethods.length:"+fordeclaredMethods.length);//8个 获取该类声明的方法,不包括父类
    
            Method forSayHello = aClass.getMethod("sayHello"); //获取public修饰的
            forSayHello.invoke(user);
    
             Method forDeclaredMethod = aClass.getDeclaredMethod("sayHello", String.class);
             forDeclaredMethod.setAccessible(true);//设置是否进行安全检查,值为true,表示反射的对象在使用时应该取消java语言的访问检查,否则则表示实施访问检查
             forDeclaredMethod.invoke(foruser,"zhangsan");
    
    
            /*
             * ------------------第三种方法--------------------------------
             * */
    
            Class<User> userClass = User.class;
            User userInstance=userClass.newInstance();//创建user实例
            userClass.getMethod("sayHello");
    
            Field[] declaredFields1 = userClass.getDeclaredFields(); //获取该类声明的属性,不包括父类
           for (Field field:declaredFields1)
           {
               System.out.println("--方法名--"+field.getName());//获取方法名
               System.out.println("--方法类型--"+field.getType());//获取方法类型
    
           }
    
            Method[] declaredMethods1 = userClass.getDeclaredMethods();//获取该类声明的方法,不包括父类
           for (Method method:declaredMethods1)
           {
               System.out.println(method.getName());
           }
    
            Method[] methods1 = userClass.getMethods();//获取所有方法,包括Object里面的
           for (Method method:methods1)
           {
               System.out.println(method.getName());//获取方法名
               System.out.println(method.getReturnType());//获取方法返回类型
               System.out.println(method.getDefaultValue());//获取方法默认值
    
           }
    
           Field fieldAge=userClass.getDeclaredField("age"); //ge是私有的 所以使用getDeclaredField获取
            if(!fieldAge.isAccessible())// 是否进行了安全检查,可返回true,不可返回false
            {
                fieldAge.setAccessible(true);//设置是否进行安全检查,值为true,表示反射的对象在使用时应该取消java语言的访问检查,否则则表示实施访问检查
            }
    
           fieldAge.set(userInstance,12);
            Object o = fieldAge.get(userInstance);
            System.out.println("age--"+o);
    
            Class<?> aClass1 = Class.forName("com.java.javaClass.common.student");
            Constructor<?> declaredConstructor = aClass1.getDeclaredConstructor(Integer.class, String.class);
            declaredConstructor.setAccessible(true);
            Object o1 = declaredConstructor.newInstance(12,"123456");
            System.out.println(o1);
    
    
            Class<?> aClass2= Class.forName("com.java.javaClass.common.student");
            Constructor<?> declaredConstructor1 = aClass1.getDeclaredConstructor();
    
            Object o2 = declaredConstructor1.newInstance();
            System.out.println(o2);
    
        }
    
    ···
  • 相关阅读:
    移动端web初体验
    网页布局框架
    CSS 3D翻转相册动画特效
    move.js框架
    js照片墙拖拽特效
    团队编程项目作业2-团队编程项目开发环境搭建过程
    课后作业-阅读任务-阅读提问-1
    团队编程项目作业3
    阅读提问-2
    结对编程项目作业5
  • 原文地址:https://www.cnblogs.com/lilihai/p/10219911.html
Copyright © 2011-2022 走看看