• 反射获得内部类


    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

    转自,每周一个技术的博客,感谢!

  • 相关阅读:
    LR实战之Discuz开源论坛——安装及简介
    LR如何利用siteScope监控MySQL性能
    初学SSH(其一)
    使用JUnit单元测试入门
    理解java中【同步】和【死锁】
    LR性能测试应用
    (28)ElasticSearch分布式架构特点
    (27)ElasticSearch 复合查询
    (06)Gitlab设置开启自启动、关闭开机自启动
    (05)安装GitLab
  • 原文地址:https://www.cnblogs.com/mzywucai/p/11053407.html
Copyright © 2020-2023  润新知