简体   繁体   English

Java泛型-类型擦除后的方法

[英]Java Generics - method after type erasure

How will the following method look after applying type erasure? 应用类型擦除后,以下方法将如何显示?

static<T> void InsertAt0(List<T> mylist, T element) {
    mylist.add(0, element);
}

will T be replaced with Object ? T替换为Object吗? or maybe type-erasure is only about generics classes? 还是类型擦除仅与泛型类有关?

Just remove all of the <T> s, and then replace any T with Object: 只需删除所有<T> ,然后用Object替换任何T

static void InsertAt0(List mylist, Object element) {
   mylist.add(0, element);
}

Note that mylist above has been erased to its raw type equivalent: List<T> became just List . 请注意,上面的mylist已被擦除为与之等效的原始类型List<T>变为List

If you had specified a wildcard bound for T, like <T extends Number> , then you would replace T with that bound instead of Object: 如果您为T指定了通配符绑定,例如<T extends Number> ,那么您将T替换为该绑定,而不是Object:

static void InsertAt0(List mylist, Number element) {
                                   ^^^^^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM