简体   繁体   English

反射不打印类中的构造函数名称

[英]reflection not printing constructor name in a class

I am trying to get the constructor name printed using reflection however its skipping the loop which is for printing the name of loop. 我正在尝试使用反射来打印构造函数名称,但是它跳过了用于打印循环名称的循环。

package reflection.com;

import java.lang.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

abstract interface FirstInterface {

    void showFirstInterface();
}

abstract interface SecondInterface {

    void showSecondInterface();
}

abstract interface ThirdInterface {

    void showThirdinterface();
}

class SuperClass implements FirstInterface {

    int x, y, z;

    SuperClass() {
        System.out.println("Super Class Constructor...");
    }

    public void showFirstInterface() {
        System.out.println("In class Super Class....");
    }
}

public class SubClass extends SuperClass implements SecondInterface, ThirdInterface {

    int a, b, c;

    SubClass() {
    }

    SubClass(int a, int b, int c) {

    }

    public void showSecondInterface() {
        System.out.println("In class Sub Class implementing Second Interface...");

    }

    public void showThirdinterface() {

        System.out.println("In class Sub Class implementing Third Interface...");
    }
}

class ReflectionTest {

    public static void main(String[] args) {
        Class class1 = SubClass.class;
        int modifier = class1.getModifiers();
        Class superClass = class1.getSuperclass();
        Class[] interfaces = class1.getInterfaces();
        Constructor[] constructor = class1.getConstructors();
        Method[] method = class1.getMethods();
        Field[] field = class1.getFields();
        System.out.println("Fully Qualified Class Name..." + class1.getName());
        System.out.println("Class Name..." + class1.getSimpleName());
        System.out.println("Class Modifier...." + Modifier.isPublic(modifier));
        System.out.println("Class Super Class....." + superClass.getName());
        System.out.println("Following are the interfaces.....");
        for (int i = 0; i < interfaces.length; i++) {
            System.out.println(interfaces[i].getName());
        }
        System.out.println("Following are the Constructor.....");
        for (int i = 0; i < constructor.length; i++) {
            System.out.println(constructor[i].getName());
        }
        System.out.println("Following are the Fields.....");
        for (int i = 0; i < field.length; i++) {
            System.out.println(field[i].getName());
        }
        System.out.println("Following are the Methods.....");
        for (int i = 0; i < method.length; i++) {
            System.out.println(method[i].getName());
        }
        for (Method method1 : method) {
            System.out.println(method1.getName());
        }
    }
}

I tried to debug the application but no use its not going into the for loop itself. 我尝试调试该应用程序,但没有使用它,而不会进入for循环本身。

Can anyone help me with this? 谁能帮我这个?

From the documentation for getConstructors() [emphasis mine]: getConstructors()的文档中[强调我的]:

Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object. 返回一个包含Constructor对象的数组,该对象反映此Class对象表示的类的所有公共构造函数。

Your class doesn't have any public constructors. 您的课程没有任何公共构造函数。 The two constructors are package-private: 这两个构造函数是程序包私有的:

public class SubClass ... {
    SubClass() {}
    SubClass(int a, int b, int c) {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM