Class的getInterfaces与getGenericInterface区别
来源:互联网 发布:小猪cms微信收银系统 编辑:程序博客网 时间:2024/06/08 07:06
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 子句中接口名顺序一致。对于数组类,接口 Cloneable 和 Serializable 以该顺序返回。
如果此对象表示一个接口,则该数组包含表示该接口直接扩展的所有接口的对象。数组中接口对象顺序与此对象所表示的接口的声明的 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) { //IStudent System.out.print("IStudent.class.getInterfaces()\t"); printInterface(IStudent.class.getInterfaces()); System.out.print("IStudent.class.getGenericInterfaces()\t"); printInterface(IStudent.class.getGenericInterfaces()); //Test System.out.print("Test.class.getInterfaces()\t" ); printInterface(Test.class.getInterfaces()); System.out.print("Test.class.getGenericInterfaces()\t"); printInterface(Test.class.getGenericInterfaces()); //Object System.out.print("Object.class.getGenericInterfaces()\t"); printInterface(Object.class.getGenericInterfaces()); System.out.print("Object.class.getInterfaces()\t" ); printInterface(Object.class.getInterfaces()); //void System.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() 2cn.test.IPerson cn.test.IWalkIStudent.class.getGenericInterfaces()3cn.test.IPerson<cn.test.Test>cn.test.IWalk<java.lang.Object> interface java.lang.CloneableTest.class.getInterfaces() 0Test.class.getGenericInterfaces()0Object.class.getGenericInterfaces()0Object.class.getInterfaces() 0void.class.getInterfaces() 0void.class.getGenericInterfaces()0int[].class.getInterfaces() 2java.lang.Cloneablejava.io.Serializableint[].class.getGenericInterfaces()2interface java.lang.Cloneableinterface java.io.Serializable
- Class的getInterfaces与getGenericInterface区别
- Class的getInterfaces与getGenericInterface区别
- getInterfaces(),getMethod(String name, Class... parameterTypes),getClass
- class与public class的区别
- a.class与a .class的区别
- Class<Object>与Class<?>的区别
- public class 与class的区别
- Class<?>与Class<T>的区别
- public class与class的区别
- public class与class的区别
- Scala class与Case class的区别
- Class.forName()与xxx.class的区别
- ng-class与class的区别
- 兔子-Class<T>与Class<?>的区别
- class 与 public class的区别
- struct与class的区别
- @class与#import的区别
- class与struct的区别
- Hibernate与 MyBatis的比较
- 往数据库添加数据后乱码
- Class类中的getEnclosing*、getDeclared*与getDeclaringClass的区别
- Class的getResource与ClassLoader的getResource路径/问题
- Class的getName、getSimpleName与getCanonicalName的区别
- Class的getInterfaces与getGenericInterface区别
- Class的 getSuperclass与getGenericSuperclass区别
- 天气API的调用
- 配置kali2.0实现远程ssh登陆
- java嵌套类与内部类
- poj 1068 模拟水题
- java入门配置
- java变量作用域Scope
- Java编程规范