更新時間:2022-05-13 10:19:17 來源:動力節(jié)點 瀏覽4168次
動力節(jié)點小編告訴大家以下兩種方法用于獲取java反射中實現(xiàn)的接口:
getGenericInterfaces() 方法用于獲取具有泛型類型信息的類實現(xiàn)的接口數(shù)組。
getInterfaces() 方法用于獲取所有已實現(xiàn)接口的數(shù)組。
形狀.java
包 com.w3spoint ;
公共 接口 形狀 {
公共 無效drawShape (字符串顏色);
}
矩形.java
包 com.w3spoint ;
公共 類 Rectangle 實現(xiàn) Shape {
private int defaultLength = 10 ;
私有 int defaultWidth = 5 ;
@覆蓋
公共 無效drawShape (字符串顏色) {
系統(tǒng)。出來。println ( "創(chuàng)建具有以下屬性的矩形:" ) ;
系統(tǒng)。出來。println ( "長度:" + defaultLength ) ;
系統(tǒng)。出來。println ( "寬度:" + defaultWidth ) ;
}
}
反射測試.java
包 com.w3spoint ;
導入 java.util.Arrays ;
公共 類ReflectionTest {
public static void main ( String args [ ] ) {
try {
Class c = Class . forName ( "com.w3spoint.Rectangle" ) ;
系統(tǒng)。出來。println ( Arrays.toString ( c.getInterfaces ( ) ) ) ; _ _ _ }捕捉(異常e ){
e. 打印堆棧跟蹤();
}
}
}
輸出
[interface com.w3spoint.Shape]
以上就是關于“Java反射獲取實現(xiàn)的接口”介紹,大家如果想了解更多相關知識,不妨來關注一下動力節(jié)點的Java基礎教程,里面有更豐富的知識等著大家去學習,希望對大家能夠有所幫助哦。