简体   繁体   中英

Java Templates, how to use two classes with the same name and different types

I have problem with templates in Java. I don't know what is wrong. I pasted the code from C# to Java and does not work.

class A<T1, T2>
{
....
}

class A<T1, T2, T3>
{
...
}

What's wrong?

You cannot have two outer classes with the same name in the same package. Try renaming one of the classes or put it in a different package. Alternatively putting one class as a nested class of another class would also work, but the question is if they really need to have the same name.

The reason for Java not allowing more than one outer class in a package with the same name is that when Java worked generic classes into its feature set, the decision was made to do this via 'type erasure'. The generic type parameter information is replaced at runtime, leaving just ordinary non-generic classes: https://docs.oracle.com/javase/tutorial/java/generics/erasure.html

C# & VB.NET also started off without generics, but when they worked generics into their feature sets, the decision was made to implement generics at the byte code level.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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