Obtaining Method Type Informatio
2019-04-17 本文已影响0人
日更专用小马甲
本系列翻译自Oracle官方教程,半翻译,半读后感性质。文末会附上原文链接。
方法的声明包括:方法名、修饰符、参数、返回值和异常。
根据是否区分泛型,对应于2组方法:
-
getReturnType()
、getParameterTypes()
、getExceptionTypes()
-
getGenericReturnType()
、getGenericParameterTypes()
、getGenericExceptionTypes()
//- 源码中函数的定义
public Constructor<T> getConstructor(Class<?>... parameterTypes)
//- returnType():
//- class java.lang.reflect.Constructor
//- genericReturnType():
//- java.lang.reflect.Constructor<T>
另外,通过不带*Generic*
的方法可以看到泛型被擦除之后,是转为类型的上界(原文是:... is replaced by the upper bound of the type variable ...)
//- 源码中函数的定义
public T cast(Object obj)
//- returnType():
//- class java.lang.Object
//- genericReturnType():
// T