Java反射专题学习-获取类的构造信息

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32575047/article/details/86002833

今天继续学习反射专题之获取类的构造函数信息:

继上篇博客来写:我们单独写一个方法来获取类的构造函数信息:(通过获取Constructor)

打印类的构造函数相关信息

   public static void ConMessage(Object object) {
        //首先获取类的'类类型'(Class Type)
        Class c = object.getClass();
        //获取类的构造函数(所有类型的:包括公有的和私有的)  getConstructors()只获取类的public 构造函数
        Constructor[] cs = c.getDeclaredConstructors();
        for (Constructor constructor : cs) {
            System.out.print(constructor.getName() + "(");
            //获取构造函数的参数类型
            Class[] parameterTypes = constructor.getParameterTypes();
            for (Class parameterType : parameterTypes) {
                System.out.print(parameterType.getName() + ",");
            }
            System.out.println(")");
        }

    }

猜你喜欢

转载自blog.csdn.net/qq_32575047/article/details/86002833