在 Java 中列出介面擴充套件的介面
可以使用 java.lang.Class.getInterfaces() 方法來確定某個物件所表示的介面所實現的介面。此方法返回實現該介面的所有介面的陣列。
演示此方法的程式如下 −
示例
package Test; import java.lang.*; import java.util.*; public class Demo { public static void main(String[] args) { listInterfaces(java.util.List.class); } public static void listInterfaces(Class c) { System.out.println("The interface is: " + c.getName()); Class[] interfaces = c.getInterfaces(); System.out.println("The Interfaces are: " + Arrays.asList(interfaces)); } }
輸出
The interface is: java.util.List The Interfaces are: [interface java.util.Collection]
現在讓我們瞭解一下該程式。
在方法 main() 中,使用 java.util.List.class 呼叫方法 listInterfaces()。一個演示此方法的程式碼片段如下 −
listInterfaces(java.util.List.class);
在方法 listInterfaces() 中,使用了方法 getName() 來列印介面的名稱。然後,使用了方法 getInterfaces() 來返回實現該介面的所有介面的陣列。然後,使用 Arrays.asList() 列印此陣列。一個演示此方法的程式碼片段如下 −
System.out.println("The interface is: " + c.getName()); Class[] interfaces = c.getInterfaces(); System.out.println("The Interfaces are: " + Arrays.asList(interfaces));
廣告