就是说,当我们定义这样一个类:
class A <T>
{
private T name;
public A(T name)
{
this.name =name;
}
public T getName()
{
return name;
}
}
然后main函数这样写:
class Main
{
public static void main(String []args)
{
A<String> obA = new A<>("test");
String name = obA.getName();
}
}
那么,编译之后就成了这样(应该说是编译之后反编译就会变成类似这样的代码)
class A
{
private Object name;
public A(Object name)
{
this.name =name;
}
public Object getName()
{
return name;
}
}
class Main
{
public static void main(String []args)
{
A obA = new A("test");
String name = (String) obA.getName();
}
}
stackoverflow:https://stackoverflow.com/questions/339699/java-generics-type-erasure-when-and-what-happens
https://stackoverflow.com/questions/313584/what-is-the-concept-of-erasure-in-generics-in-java
另外可以参考: https://blog.csdn.net/briblue/article/details/76736356