Class的getInterfaces与getGenericInterface区别
| ?Class<?>[] | getInterfaces()??? ? ? ? ? ? ? ?確定此對象所表示的類或接口實現的接口。 |
如果此對象表示一個類,則返回值是一個數組,它包含了表示該類所實現的所有接口的對象。數組中接口對象順序與此對象所表示的類的聲明的 implements 子句中接口名順序一致。例如,給定聲明:
class Shimmer implements FloorWax, DessertTopping { ... } 設 s 的值為 Shimmer 的一個實例;表達式:如果此對象表示一個接口,則該數組包含表示該接口擴展的所有接口的對象。數組中接口對象順序與此對象所表示的接口的聲明的 extends 子句中接口名順序一致。
如果此對象表示一個不實現任何接口的類或接口,則此方法返回一個長度為 0 的數組。
如果此對象表示一個基本類型或 void,則此方法返回一個長度為 0 的數組。?
?
二、getGenericInterface ?返回直接實現的接口(包含泛型參數)
| ?Type[] | getGenericInterfaces()?? ? ? ? ? ? ? ? 返回表示某些接口的 Type,這些接口由此對象所表示的類或接口直接實現。 |
返回表示某些接口的 Type,這些接口由此對象所表示的類或接口直接實現。
如果超接口是參數化類型,則為它返回的 Type 對象必須準確反映源代碼中所使用的實際類型參數。如果以前未曾創建表示每個超接口的參數化類型,則創建這個類型。有關參數化類型創建過程的語義,請參閱 ParameterizedType 聲明。
如果此對象表示一個類,則返回一個包含這樣一些對象的數組,這些對象表示該類實現的所有接口。數組中接口對象順序與此對象所表示的類的聲明的 implements 子句中接口名順序一致。對于數組類,接口 Cloneable 和 Serializable 以該順序返回。
如果此對象表示一個接口,則該數組包含表示該接口直接擴展的所有接口的對象。數組中接口對象順序與此對象所表示的接口的聲明的 extends 子句中接口名順序一致。
如果此對象表示一個不實現任何接口的類或接口,則此方法返回一個長度為 0 的數組。
如果此對象表示一個基本類型或 void,則此方法返回一個長度為 0 的數組。
?
返回:?
代碼測試:
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区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Class的getName、getSim
- 下一篇: 避坑刻不容缓!冰火两重天的电商游戏主机市