我正在尝试使用反射打印构造函数名称,但是它跳过了用于打印循环名称的循环。
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循环。
有谁能帮我一下吗?来自getConstructors()
的文档:
返回一个包含
Constructor
对象的数组,该数组反映了这个Class
对象所表示的类的所有公共构造函数。
你的类没有任何公共构造函数。这两个构造函数是package-private:
public class SubClass ... {
SubClass() {}
SubClass(int a, int b, int c) {}