泛型反射

leyestd 2012-09-24 03:28:07
public static void printType(Type type, boolean isDefinition)
{
if (type instanceof Class)
{
Class<?> t = (Class<?>) type;
System.out.print(t.getName());
}
else if (type instanceof TypeVariable)
{
TypeVariable<?> t = (TypeVariable<?>) type;
System.out.print(t.getName());
if (isDefinition)
printTypes(t.getBounds(), " extends ", " & ", "", false);
}
else if (type instanceof WildcardType)
{
WildcardType t = (WildcardType) type;
System.out.print("?");
printTypes(t.getUpperBounds(), " extends ", " & ", "", false);
printTypes(t.getLowerBounds(), " super ", " & ", "", false);
}
else if (type instanceof ParameterizedType)
{
ParameterizedType t = (ParameterizedType) type;
Type owner = t.getOwnerType();
if (owner != null)
{
printType(owner, false);
System.out.print(".");
}
printType(t.getRawType(), false);
printTypes(t.getActualTypeArguments(), "<", ", ", ">", false);
}
else if (type instanceof GenericArrayType)
{
GenericArrayType t = (GenericArrayType) type;
System.out.print("");
printType(t.getGenericComponentType(), isDefinition);
System.out.print("[]");
}

}

这里
type instanceof WildcardType
type instanceof ParameterizedType
type instanceof TypeVariable
type instanceof GenericArrayType
分别代表什么
...全文
231 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhaoming262350 2012-09-24
  • 打赏
  • 举报
回复
instanceof 用来判断这个对象属不属于后面的这个类,如果这个对象是类的对象,返回true,否则返回false
江湖啊江湖 2012-09-24
  • 打赏
  • 举报
回复
4楼我 -1..........
jijihahalala 2012-09-24
  • 打赏
  • 举报
回复
instanceof 的作用,就是判断前面的是否是后面的对象。
比如Dog extends Animal 那么 Dog instanceof Animal确实如此,为true
Alexander 2012-09-24
  • 打赏
  • 举报
回复
等价于C#中的is
  • 打赏
  • 举报
回复
instanceof 的作用,就是判断前面的是否是后面的对象。
展S开K双Y翼 2012-09-24
  • 打赏
  • 举报
回复
对象 instanceof 类
这是判断对象属于不属于这个类,是不是这个类的对象

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧