zoukankan      html  css  js  c++  java
  • 反射

     1、用反射的加载的类去创建实例,并实现将创建的实例去调用此类中的方法。

    String name="Com.huyanlan.javaTest.Person";
            @SuppressWarnings("rawtypes")
            Class clazz=Class.forName(name);
            Object obj=clazz.newInstance();//这是一个创建实例的方法,就是用类加载的方法进行。
            ((Person) obj).setName("Tom");

    2、简单的一些关于反射的调用类的方法,以及怎么去执行私有方法。

    public void Studenttest() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException{
            Class clazz=Class.forName("Com.huyanlan.javaTest.Student");
            Method method_1=clazz.getDeclaredMethod("method1", int.class);//""里面的名字是对象里的方法,后面则是类型的封装类。
            
            Object obj=clazz.newInstance();//创建实例
            method_1.setAccessible(true);//允许通过以下的语句去执行
            method_1.invoke((Student)obj, 3);//已经执行了这个方法。
        //    System.out.println(((Student) obj).getN());
        //    System.out.println(method_1);
        }

    3、怎么利用反射加载私有属性变量,并做相应的处理。

    public void StudentTest1() throws ClassNotFoundException, NoSuchFieldException, SecurityException, InstantiationException, IllegalAccessException{
            Class clazz=Class.forName("Com.huyanlan.javaTest.Student");
        //    System.out.println(clazz);
            Person person;
        //    System.out.println();
            Field field=clazz.getDeclaredField("Name");
            Object obj=clazz.newInstance();
        //    System.out.println(obj);
            field.setAccessible(true);
            field.set(obj, "胡焰兰");
        //    System.out.println(((Student) obj).getName());        
        }

    4、利用反射,加载注释。

    public void AnnotationTest() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException{
            Class clazz=Class.forName("Com.huyanlan.javaTest.Student");
            Method method=clazz.getDeclaredMethod("setGrade", int.class);
            System.out.println(method);
            Annotation annotation= method.getAnnotation(GradeValue.class);
            
            System.out.println(annotation);
            int val=30;
            if(annotation!=null){
                if(annotation instanceof GradeValue){
                    GradeValue gradeValue=(GradeValue) annotation;
                    if(val>gradeValue.max()||val<gradeValue.min()){
                        System.out.println("赋值不合法");
                    }
                }
            }
        }

    5、利用反射获得类的泛型类型。

    public static Class getReflectGeneric(Class clazz,int index){
            Type type=clazz.getGenericSuperclass();
            if(!(type instanceof ParameterizedType)){
                return Object.class;
            }
            Type[] args=((ParameterizedType)type).getActualTypeArguments();
            if(args[index]==null &&args.length<0){
                return Object.class;
            }
            
            //System.out.println(type);
            return (Class) args[index];
        }
  • 相关阅读:
    第二节:如何正确使用WebApi和使用过程中的一些坑
    nodejs中function*、yield和Promise的示例
    关于nodejs访问mysql的思考
    nodejs使用log4js记录日志
    nodejs初识
    Spring学习笔记(入门)
    mybatis使用注解开发
    MyBatis配置文件中的常用配置
    using 自动释放资源示例
    Java将byte[]和int的互相转换
  • 原文地址:https://www.cnblogs.com/baiyangLI/p/6421965.html
Copyright © 2011-2022 走看看