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

时间:2013-03-02 08:53:36

标签: java reflection

我正在尝试使用反射打印构造函数名称,但是它会跳过用于打印循环名称的循环。

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());
        }
    }
}

我尝试调试应用程序,但没有使用它不进入for循环本身。

任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:6)

来自getConstructors() [强调我的]的文档:

  

返回一个包含Constructor个对象的数组,这些对象反映了此Class对象所代表的类的所有 public 构造函数。

您的课程没有任何公共构造函数。这两个构造函数是包私有的:

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