• 反射


     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];
        }
  • 相关阅读:
    Oracle合并某一列
    button的FlatStyle和FlatAppearance属性
    Winform中ComBox大小设置
    项目添加程序集的引用后老是报错
    VS中文档大纲视图的作用
    将DotNetBar添加到工具箱中
    Win10设置vs2010总是以管理员身份运行
    SQL SERVER2008 打开脚本总是报“未能完成操作,存储空间不足”
    如何用vs2013开发人员命令提示工具执行一个方法(一个简单的demo)
    windows mysql 8.0 安装 解压版
  • 原文地址:https://www.cnblogs.com/baiyangLI/p/6421965.html
Copyright © 2020-2023  润新知