更新時(shí)間:2022-05-13 10:19:17 來源:動力節(jié)點(diǎn) 瀏覽4121次
動力節(jié)點(diǎn)小編告訴大家以下兩種方法用于獲取java反射中實(shí)現(xiàn)的接口:
getGenericInterfaces() 方法用于獲取具有泛型類型信息的類實(shí)現(xiàn)的接口數(shù)組。
getInterfaces() 方法用于獲取所有已實(shí)現(xiàn)接口的數(shù)組。
形狀.java
包 com.w3spoint ;
公共 接口 形狀 {
公共 無效drawShape (字符串顏色);
}
矩形.java
包 com.w3spoint ;
公共 類 Rectangle 實(shí)現(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 ;
導(dǎo)入 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]
以上就是關(guān)于“Java反射獲取實(shí)現(xiàn)的接口”介紹,大家如果想了解更多相關(guān)知識,不妨來關(guān)注一下動力節(jié)點(diǎn)的Java基礎(chǔ)教程,里面有更豐富的知識等著大家去學(xué)習(xí),希望對大家能夠有所幫助哦。
相關(guān)閱讀
初級 202925
初級 203221
初級 202629
初級 203743