zoukankan      html  css  js  c++  java
  • 反射详解四

    • 反射的get方法
      /*
            getter方法
            o:要操作类的对象
            args:属性名
         */
        public static <T> T getXxx(T o,String args) throws NoSuchFieldException {
            Class cls = o.getClass();
            //判断该属性是否存在
            Field field = field = cls.getDeclaredField(args);
            if(field == null){
                field = cls.getField(args);
            }
            if(field == null){
                return null;
            }
    
    
            String fieldName = "get"+args.substring(0,1).toUpperCase()+(args.length()>1?args.substring(1):"");
            Method method = null;
            try {
                method = cls.getMethod(fieldName);
                return (T)method.invoke(o);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            return null;
        }
    •  反射的set方法
    /*
            setter方法
            o:要操作类的对象
            args:属性名
            attributeValue:属性值
         */
        public static void setXxx(Object o,String args,Object attributeValue){
            Class cls = o.getClass();
            //判断该属性是否存在
            Field field = null;
            try {
                field = cls.getDeclaredField(args);
                if(field == null){
                    field = cls.getField(args);
                }
                if(field == null){
                    return;
                }
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
    
            String fieldName = "set"+args.substring(0,1).toUpperCase()+(args.length()>1?args.substring(1):"");
            Method method = null;
            try {
                method = cls.getMethod(fieldName,attributeValue.getClass());
                method.invoke(o,attributeValue);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    •  反射的相关方法
    getName():获得类的完整名字。  
    newInstance():通过类的不带参数的构造方法创建这个类的一个对象。
    
    getFields():获得类的public类型的属性。  
    getDeclaredFields():获得类的所有属性。
    
    getMethods():获得类的public类型的方法。  
    getDeclaredMethods():获得类的所有方法。  
    getMethod(String name, Class[] parameterTypes):获得类的特定方法。
    
    getModifiers()和Modifier.toString():获得属修饰符,例如private,public,static等  
    getReturnType():获得方法的返回类型  
    getParameterTypes():获得方法的参数类型
    
    getConstructors():获得类的public类型的构造方法。  
    getConstructor(Class[] parameterTypes):获得类的特定构造方法。
    
    getSuperclass():获取某类的父类  
    getInterfaces():获取某类实现的接口
    • 获取权限名
    Modifier.toString(field.getModifiers())
    Modifier.toString(method.getModifiers())

     getFields()            只能获取public的字段,包括父类的。
    getDeclaredFields()    只能获取自己声明的各种字段,包括public,protected,private。
    getFields()和 getDeclaredFields(),返回的都是Field对象,获取名称直接field.getName(), 但是属性值则是field.get(Object),这个object是该field所属的!!!

    故乡明
  • 相关阅读:
    [转载]VC补遗之Profile篇
    [原创]百度之星2009初赛第二场第四题解答
    [总结]QT在CODE:BLOCKS中的配置
    [原创]自己写的一个简单的程序日志记录类
    [原创]QT动态加载UI文件注意事项
    window版本信息资源格式
    [原创]滚动条滚动范围的问题总结
    ofstream奇怪问题解决方法
    [转载]最小矩形(rec1)的解题报告
    oracle数据库用户之间授权
  • 原文地址:https://www.cnblogs.com/luweiweicode/p/14151527.html
Copyright © 2011-2022 走看看