• 对反射用法的初探


    以前看到别人的一小段方法,使用反射来写的,对反射不是很熟悉,愣是没看懂,今天有幸再次翻出来看了一下,API的小用法。
    此次弄清除2个问题:
    1、invoke()方法的作用
    2、getMethod(obj,new Class[]{x.class,y.class,...});的用法
    解答: 自我理解
    1、invoke()方法的作用,就是执行对象要调用的方法
    2、getMethod()方法中第二个参数是一个class的数组,用以标识参数类型
    详细测试示例如下:
    Class<?> c = Ref.class;
    Object o = c.newInstance();
    //第一个实参为方法名,第二个实参为方法参数类型对应的class对象
    Method nameMethod = c.getDeclaredMethod("setName",String.class);
    nameMethod.invoke(o,"name1");//运行方法
    nameMethod = c.getDeclaredMethod("getName", null);
    String name = (String) nameMethod.invoke(o, null);
    /**
     * @description 测试用例
     * @version 1.0   
     */
    public class Ref {
    
        private String name;
        
        private Integer age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
        
        public String say(String name,Integer age){
            return "My name id "+name+" and my age is " + age;
        }
        
        public String say(){
            return "My name id "+this.name+" and my age is " + this.age;
        }
        
    }
     1 /**
     2  * @description 反射学习方式
     3  * @version 1.0   
     4  */
     5 public class RefTest {
     6 
     7     public  void putValue(String name,Object value) throws Exception{  
     8            Class c = this.getClass();  
     9            Class v = value.getClass();  
    10            String setMethodName = "set"
    11                    + name.substring(0, 1).toUpperCase()
    12                    + name.substring(1);
    13            Method m = c.getMethod(setMethodName, new Class[]{v});  
    14            m.invoke(this, new Object[]{value});  
    15      } 
    16     
    17     
    18     public static void main(String[] args) throws Exception{
    19         
    20         Class<?> c = Ref.class;
    21         Object o = c.newInstance();
    22         //单参、多参和无参
    23         Method nameMethod = c.getDeclaredMethod("setName",String.class);
    24         nameMethod.invoke(o,"name1");//运行方法
    25         Method ageMethod = c.getDeclaredMethod("setAge", Integer.class);
    26         ageMethod.invoke(o, 18);
    27         Class cs = String.class; Class ci = Integer.class;
    28         Method sayMethod2 = c.getDeclaredMethod("say", new Class[]{cs,ci});
    29         String say2 = (String)sayMethod2.invoke(o, "name2",16);
    30         System.out.println(say2);
    31         
    32     }
    33 
    34 }
  • 相关阅读:
    狼人杀BETA阶段计划简介
    Werewolf流程分析
    Alpha阶段项目Postmortem
    Alpha阶段项目展示
    龙威零式_团队项目例会记录_24
    数据获取以及处理系统 --- 功能规格说明书V2.0
    项目Postmortem
    龙威零式_团队项目例会记录_23
    Alpha版本发布说明
    Alpha版本项目展示
  • 原文地址:https://www.cnblogs.com/cfb513142804/p/6531210.html
Copyright © 2020-2023  润新知