通用方法有界类型参数和类型擦除 [英] Generic method bounded type parameter and type erasure

查看:90
本文介绍了通用方法有界类型参数和类型擦除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般的方法如下:

  static< E,K extends E> void someMethod(K k,E [] e){} 

我推测擦除时,擦除类型将是:
$ b $ pre $ static void someMethod(Object k,Object [] e){}

只是很好奇类型参数在类型擦除后如何知道约束条件?这个类型参数K是有界的吗?

解决方案

你对擦除是正确的。实际上,运行时并不知道约束条件。只有编译器才行。


A generic method as per below:

static <E, K extends E> void someMethod(K k, E[] e) {}

I presumed upon erasure, the erasure type would be:

static void someMethod(Object k, Object[] e) {}

Just curious how would the type parameter knows the constraints after type erasure? That type parameter K is bounded to E?

解决方案

You are correct about the erasure. Actually, the runtime doesn't know about the constraints. Only the compiler does.

这篇关于通用方法有界类型参数和类型擦除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆