如何编写一个在java中使用两个相同类型参数的泛型方法? [英] How to write a generic method that takes two arguments of the same types in java?
问题描述
当我注意到下面的代码编译时没有警告并且输出 Integer / String
:
public final class GenericsTest {
private static< T> void方法(T arg1,T arg2){
System.out.println(arg1.getClass()。getSimpleName());
System.out.println(arg2.getClass()。getSimpleName());
public static void main(String [] args){
method(1,1);
}
}
我预计编译错误。
这段代码编译的原因是什么?
确保参数具有相同类型的正确方法是什么? p>
编辑:有界的类型参数怎么样?我能想到的最好的是:
private static< T,U extends T> void method(T arg1,U arg2){
System.out.println(arg1.getClass()。getSimpleName());
System.out.println(arg2.getClass()。getSimpleName());
}
不幸的是,java不允许循环约束。 < T extends U,U extends T>
不会编译。这是一个死路一条?
编译的原因是因为Java会推断出传入的参数的最具体的超类型,在这种情况下, Object
Serializable&可比< ;?扩展了Serializable&可比< ;?在将
1
装箱到整数
后扩展Comparable<?>>> 和1
作为字符串
。
没有泛型:
private static void method(Number arg1,Number arg2){
即使没有泛型,您也可以传入整数
和一个 Double
。
只有当问题类型是 final
时,才可以这样做,没有泛型:
private static void method(String arg1,String arg2){
//是的,它们是两个字符串,保证。
有一种边界情况,我可以想到确保它们是确切类型的泛型。如果你有一个 final 类,并且你放置了一个上限,那么你可以将它限制在同一个类中。
public< T extends MyFinalClass> void method(T arg1,T arg2){
//是的,它们都是MyFinalClasses
}
但是你可以做同样的事情没有泛型。
public void method(MyFinalClass arg1,MyFinalClass arg2){
//是的,它们都是MyFinalClasses
}
I was very surprised when I noticed that following code compiles without warnings and prints Integer / String
:
public final class GenericsTest {
private static <T> void method(T arg1, T arg2) {
System.out.println(arg1.getClass().getSimpleName());
System.out.println(arg2.getClass().getSimpleName());
}
public static void main(String[] args) {
method(1, "1");
}
}
I expected a compilation error.
Is there a reason why this code compiles?
What is the correct way to ensure that arguments have the same type?
Edit: What about bounded type parameters? The best I can think of is this:
private static <T, U extends T> void method(T arg1, U arg2) {
System.out.println(arg1.getClass().getSimpleName());
System.out.println(arg2.getClass().getSimpleName());
}
Unfortunately, java doesn't allow cyclic constraints. <T extends U, U extends T>
doesn't compile. Is this a dead end?
The reason that this compiles is because Java will infer the most specific supertype of the arguments passed in, in this case, Object
Serializable & Comparable<? extends Serializable & Comparable<? extends Comparable<?>>>
, after 1
is boxed to Integer
and "1"
is passed as a String
.
Without generics:
private static void method(Number arg1, Number arg2) {
Even without generics, you can pass in an Integer
and a Double
.
Only if the type in question is final
can you do this, without generics:
private static void method(String arg1, String arg2) {
// Yes, they're both Strings, guaranteed.
There is one edge case with generics that I can think of to ensure that they are the exact type. If you have a final
class, and you place an upper bound, then you can restrict it to that same class.
public <T extends MyFinalClass> void method(T arg1, T arg2) {
// Yes, they're both MyFinalClasses
}
But then you could do the same thing without generics.
public void method(MyFinalClass arg1, MyFinalClass arg2) {
// Yes, they're both MyFinalClasses
}
这篇关于如何编写一个在java中使用两个相同类型参数的泛型方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!