码迷,mamicode.com
首页 > 编程语言 > 详细

java 反射

时间:2018-08-28 20:18:12      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:实例   获取   getc   java   declare   方法调用   构造方法   tde   row   

public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
        
        
        //加载class对象
        Class clazz = Class.forName("com.oa.model.wxlog.WxLog");
        
        System.out.println("---------------------------获取所有的公共构造方法");
        Constructor[] construArray= clazz.getConstructors();
        for(Constructor c:construArray) {
            System.out.println(c);
        }
        
        System.out.println("----------------------------获取所有的构造方法");
        Constructor[] d= clazz.getDeclaredConstructors();
        for(Constructor c:construArray) {
            System.out.println(c);
        }
        System.out.println("------------------------------获取所有的属性");
        Field[] filed=clazz.getDeclaredFields();
        for(Field c:filed) {
            System.out.println(c);
        }
        System.out.println("------------------------------获取所有的方法");
        Method[] methods=clazz.getDeclaredMethods();
        for(Method c:methods) {
            System.out.println(c);
        }
        
        System.out.println("----------------------------------方法执行");
            //创建对象实例
            Object o=clazz.newInstance();
            //传入方法名,以及方法中的参数
            Method mo=clazz.getMethod("getOperatortype", null);
            //方法调用,传入对象以及方法参数
            mo.invoke(o, null);
            
            Method mo1=clazz.getMethod("setOperatortime", Date.class);
            mo1.invoke(o, new Date());
    }

 

java 反射

标签:实例   获取   getc   java   declare   方法调用   构造方法   tde   row   

原文地址:https://www.cnblogs.com/iscys/p/9550118.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!