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) {
        //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()	        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
posted @ 2017-04-27 10:50  茅坤宝骏氹  阅读(3552)  评论(0编辑  收藏  举报