日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Class的getInterfaces与getGenericInterface区别

發布時間:2023/12/3 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Class的getInterfaces与getGenericInterface区别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、getInterfaces ? 返回直接實現的接口( 由于編譯擦除,沒有顯示泛型參數
?Class<?>[]

getInterfaces()???

? ? ? ? ? ? ?確定此對象所表示的類或接口實現的接口。

確定此對象所表示的類或接口實現的接口。

如果此對象表示一個類,則返回值是一個數組,它包含了表示該類所實現的所有接口的對象。數組中接口對象順序與此對象所表示的類的聲明的 implements 子句中接口名順序一致。例如,給定聲明:

class Shimmer implements FloorWax, DessertTopping { ... } 設 s 的值為 Shimmer 的一個實例;表達式:
s.getClass().getInterfaces()[0]; 的值為表示 FloorWax 接口的 Class 對象;
s.getClass().getInterfaces()[1]; 的值為表示 DessertTopping 接口的 Class 對象。

如果此對象表示一個接口,則該數組包含表示該接口擴展的所有接口的對象。數組中接口對象順序與此對象所表示的接口的聲明的 extends 子句中接口名順序一致。

如果此對象表示一個不實現任何接口的類或接口,則此方法返回一個長度為 0 的數組。

如果此對象表示一個基本類型或 void,則此方法返回一個長度為 0 的數組。?

返回:
該類所實現的接口的一個數組。

?

二、getGenericInterface ?返回直接實現的接口(包含泛型參數

?Type[]

getGenericInterfaces()??

? ? ? ? ? ? ? 返回表示某些接口的 Type,這些接口由此對象所表示的類或接口直接實現。

返回表示某些接口的 Type,這些接口由此對象所表示的類或接口直接實現。

如果超接口是參數化類型,則為它返回的 Type 對象必須準確反映源代碼中所使用的實際類型參數。如果以前未曾創建表示每個超接口的參數化類型,則創建這個類型。有關參數化類型創建過程的語義,請參閱 ParameterizedType 聲明。

如果此對象表示一個類,則返回一個包含這樣一些對象的數組,這些對象表示該類實現的所有接口。數組中接口對象順序與此對象所表示的類的聲明的 implements 子句中接口名順序一致。對于數組類接口 CloneableSerializable 以該順序返回。

如果此對象表示一個接口,則該數組包含表示該接口直接擴展的所有接口的對象。數組中接口對象順序與此對象所表示的接口的聲明的 extends 子句中接口名順序一致。

如果此對象表示一個不實現任何接口的類或接口,則此方法返回一個長度為 0 的數組。

如果此對象表示一個基本類型或 void,則此方法返回一個長度為 0 的數組。

?

返回:
此類所實現的接口的一個數組
拋出:
GenericSignatureFormatError - 如果常規類簽名不符合 Java Virtual Machine Specification, 3rd edition 規定的格式
TypeNotPresentException - 如果任意常規超接口引用不存在的類型聲明
MalformedParameterizedTypeException - 如果任意常規超接口引用的參數化類型由于某種原因無法實例化

?

代碼測試:

package cn.test;import java.lang.reflect.Type;public class Test {public static void printInterface(Class<?>[] cs) {System.out.print(cs.length+"\t");for(Class<?> c :cs){System.out.print(c.getCanonicalName()+"\t");}System.out.println();}public static void printInterface(Type[] cs) {System.out.print(cs.length+"\t");for(Type c :cs){System.out.print(c.toString()+"\t");}System.out.println();}public static void main(String[] args) {//IStudentSystem.out.print("IStudent.class.getInterfaces()\t");printInterface(IStudent.class.getInterfaces()); System.out.print("IStudent.class.getGenericInterfaces()\t");printInterface(IStudent.class.getGenericInterfaces());//TestSystem.out.print("Test.class.getInterfaces()\t" );printInterface(Test.class.getInterfaces());System.out.print("Test.class.getGenericInterfaces()\t");printInterface(Test.class.getGenericInterfaces());//ObjectSystem.out.print("Object.class.getGenericInterfaces()\t");printInterface(Object.class.getGenericInterfaces());System.out.print("Object.class.getInterfaces()\t" );printInterface(Object.class.getInterfaces());//voidSystem.out.print("void.class.getInterfaces()\t");printInterface(void.class.getInterfaces());System.out.print("void.class.getGenericInterfaces()\t");printInterface(void.class.getGenericInterfaces());//int[]System.out.print("int[].class.getInterfaces()\t");printInterface(int[].class.getInterfaces());System.out.print("int[].class.getGenericInterfaces()\t");printInterface(int[].class.getGenericInterfaces());}}interface IPerson<T> {} interface IWalk<T> {} interface IStudent extends IPerson<Test>,IWalk<Object>,Cloneable{}

?

?

運行結果:

IStudent.class.getInterfaces() 2 cn.test.IPerson cn.test.IWalk IStudent.class.getGenericInterfaces() 3 cn.test.IPerson<cn.test.Test> cn.test.IWalk<java.lang.Object> interface java.lang.Cloneable Test.class.getInterfaces() 0 Test.class.getGenericInterfaces() 0 Object.class.getGenericInterfaces() 0 Object.class.getInterfaces() 0 void.class.getInterfaces() 0 void.class.getGenericInterfaces() 0 int[].class.getInterfaces() 2 java.lang.Cloneable java.io.Serializable int[].class.getGenericInterfaces() 2 interface java.lang.Cloneable interface java.io.Serializable

總結

以上是生活随笔為你收集整理的Class的getInterfaces与getGenericInterface区别的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。