2013-03-02 74 views
-1

我想獲取使用反射打印的構造函數名稱,但它跳過循環打印循環的名稱。反射不打印類中的構造函數名稱

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循環本身。

任何人都可以幫助我嗎?

+0

我在這裏看不到一個循環.. – christopher 2013-03-02 08:55:09

+5

A *很多*你的代碼是完全不相干的問題 - 以及格式化令人難以置信的糟糕。請將您的問題簡化爲* short *,但完整的程序只*包括與您的問題相關的代碼,然後顯示實際輸出和預期輸出。 – 2013-03-02 08:55:18

回答

6

從文檔getConstructors() [重點礦山]:

返回包含Constructor對象反射所有公共通過此Class對象表示的類的構造器的陣列。

您的班級沒有任何公共構造函數。這兩個構造是包私有:

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

所以如果構造函數是包私有的...那麼我們如何才能得到構造函數的名稱 – 2013-03-02 09:50:11

+0

@Deepak_Sharma:請把它作爲一個單獨的問題發佈。 – NPE 2013-03-02 09:50:59

相關問題