Java Class 类 getGenericInterfaces()方法及示例
Class类getGenericInterfaces()方法
getGenericInterfaces()方法在java.lang包中可用。
getGenericInterfaces()方法用于返回一个数组,该数组表示由类实现的接口或由此对象表示的接口。
getGenericInterfaces()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
返回接口数组时,getGenericInterfaces()方法可能会引发异常。
GenericSignatureFormatError:当通用类签名与JVM规范中给定的格式不匹配时,可能会引发此异常。
TypeNotPresentException:当任何通用父接口引用不存在的类型时,可能会引发此异常。
MalformedParameterizedTypeException:当任何通用父接口引用无法初始化的参数化类型时,可能会引发此异常。
语法:
public Type[] getGenericInterfaces();
参数:
它不接受任何参数。
返回值:
此方法的返回类型为Type[],它返回由该类实现的接口数组。
示例
//Java程序演示示例
//类的Type[]getGenericInterfaces()方法
import java.lang.reflect.*;
public class GetGenericInterfacesOfClass {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
Class cl = sb.getClass();
//它返回一个接口对象数组,该数组是
//由Class实现
Type[] ty = cl.getGenericInterfaces();
if (ty.length != 0)
for (int i = 0; i < ty.length; ++i) {
System.out.print("Interfaces implemented by StringBuilder: ");
System.out.println(ty[i].toString());
}
else
System.out.println("No interface implemented by StringBuilder ");
}
}输出结果
Interfaces implemented by StringBuilder: interface java.io.Serializable Interfaces implemented by StringBuilder: java.lang.Comparable<java.lang.StringBuilder> Interfaces implemented by StringBuilder: interface java.lang.CharSequence