zoukankan      html  css  js  c++  java
  • 反射获取内部类以及调用内部类方法

    1.反射调用类方法用invoke即可,但是内部类的话还是需要琢磨一番

    2.调用invoke方法需要获得参数,即类实例,通过构造函数来获得

    先写个大小类:

    /**
     * Created by garfield on 2016/11/18.S
     */
    public class OuterClass {
        public void print(){
            System.out.println("i am Outer class");
        }
         class InnerClass{
             void print2(){
                System.out.println("i am inner class");
            }
        }
    }

    调用:

    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    /**
     * Created by garfield on 2016/11/18.
     */
    public class TestClass {
        public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
            Class c = Class.forName("com.newland.sri.utep.OuterClass");
            //通过方法名获取方法
            Method method = c.getDeclaredMethod("print");
            //调用外部类方法
            method.invoke(c.newInstance());
            //内部类需要使用$分隔
            Class c2 = Class.forName("com.newland.sri.utep.OuterClass$InnerClass");
            Method method2 = c2.getDeclaredMethod("print2");
            //通过构造函数创建实例,如果没有重写构造方法则不管是不是获取已声明构造方法,结果是一样的
            method2.invoke(c2.getDeclaredConstructors()[0].newInstance(c.newInstance()));
        }
    }

    2.如果出现了不同情况,也就是构造方法被重写了,因为获取的实例不同,其构造方法也不同,所以要添加上参数

    更改一下类:

    /**
     * Created by garfield on 2016/11/18.S
     */
    public class OuterClass {
        public void print(){
            System.out.println("i am Outer class");
        }
         class InnerClass{
             InnerClass(String a){
                 
             }
             void print2(){
                System.out.println("i am inner class");
            }
        }
    }

    中间重写了内部类的构造方法,这时候做相应更改:

    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    /**
     * Created by garfield on 2016/11/18.
     */
    public class TestClass {
        public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
            Class c = Class.forName("com.newland.sri.utep.OuterClass");
            //通过方法名获取方法
            Method method = c.getDeclaredMethod("print");
            //调用外部类方法
            method.invoke(c.newInstance());
            //内部类需要使用$分隔
            Class c2 = Class.forName("com.newland.sri.utep.OuterClass$InnerClass");
            Method method2 = c2.getDeclaredMethod("print2");
            //这时候不能用c2.getConstructors(),已经不存在未声明的构造方法,所以这样写是错的
            method2.invoke(c2.getDeclaredConstructors()[0].newInstance(c.newInstance(),"a"));
        }
    }

    结果:

    i am Outer class
    i am inner class
  • 相关阅读:
    【原创】μC/OS Ⅱ工程中MAKETEST.BAT文件的改进与优化
    网络号和主机号等的分析和计算
    QQ电脑管家加速升级版便携版
    ARM发展历史
    μC/OS Ⅱ之任务就绪表的操作_2012.5.31
    删除word中的多余空行
    μC/OS II安装调试指南(带图解说明)
    WPF简单的User Control
    How to Databind SelectedItems of the ListPicker and RecurringDaysPicker
    数组排序
  • 原文地址:https://www.cnblogs.com/garfieldcgf/p/6149285.html
Copyright © 2011-2022 走看看