泛型类型中的参数化方法 [英] Parameterized methods in generic class type

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

问题描述

我有一个相当简单的问题。我无法通过搜索找到答案。



这两个代码片段有区别吗?

Fragment1:

  public class BinaryTree< ; T扩展了Comparable< super T>> {
...
public< E extends T> void add(E value){
...
}

public< E extends T> void add(E value,Node node){
...
}
...
}

Fragment2:

  public class BinaryTree< T extends Comparable< super T>> {
...
public void add(T value){
...
}

public void add(T value,Node node) {
...
}
...
}

Fragment1明确指定参数必须是 T 类型或 T 类型的子类型。



Fragment2指定参数的类型必须是 T 。但是从我的知识和经验来看,我认为我也可以在这里提供一个 T 的子类型。与fragment1相同。



我查看了这两个片段的反汇编字节代码。确实有一个区别:

 <公共< E延伸T> void add(E); 
---
> public void add(T);

这只是反映源代码...



我只是不明白这个意思。我也无法找到一个示例应用程序,它显示了差异。



感谢您的评论。

解决方案

在这种情况下,没有区别。让我们举一个例子 BinaryTree< Number> 并尝试添加一个整数

  BinaryTree< Number> numTree = new BinaryTree<>(); 
整数i = 1;
numTree.add(i);

对于片段1, E 可以评估为整数,但这在这种情况下是多余的。 Integer Number ,您可以指定 Number for E

  numTree <编号>添加(ⅰ); 

由于这个原因,第二个片段与第一个片段没什么区别,类型参数。




在某些情况下,可能会使用其他类型参数。想象一下,你想要返回传入的值:

  public< E extends T> E增加(E值){
...
返回值;
}

public< E extends T> E add(E值,Node节点){
...
返回值;
}

现在这对调用者很有用:

  Integer i2 = numTree.add(i); 

使用第二个代码片段是不可能的, numTree.add即使您传入了整数,也只能返回 Number


I have a rather simple question. I can't find an answer by searching though.

Is there a difference in these two code-fragments? And what is the difference?

Fragment1:

public class BinaryTree<T extends Comparable<? super T>> {
   ...
   public <E extends T> void add(E value) {
      ...
   }

   public <E extends T> void add(E value, Node node) {
      ...
   }
   ...
}

Fragment2:

public class BinaryTree<T extends Comparable<? super T>> {
   ...
   public void add(T value) {
      ...
   }

   public void add(T value, Node node) {
      ...
   }
   ...
}

Fragment1 specifies explicitly, that the parameter value must be either of type T or a subtype of type T.

Fragment2 specifies, that the parameter value must be of type T. But from my little knowledge and experience I think that I can also supply a subtype of T here. So same as fragment1.

I looked at the disassembled byte codes of these two fragments. Indeed there is a difference:

<   public <E extends T> void add(E);
---
>   public void add(T);

That just reflects the source code ...

I just don't get the meaning. And I also can't find an example application, which shows the difference.

Thanks for comments.

解决方案

In this case there is no difference. Let's take for example a BinaryTree<Number> and try adding an Integer:

BinaryTree<Number> numTree = new BinaryTree<>();
Integer i = 1;
numTree.add(i);

With fragment 1, E may evaluate to Integer, but that's superfluous in this case. Integer is a Number, and you could just as well specify Number for E:

numTree.<Number>add(i);

For this reason the second snippet is no different than the first, and less confusing for not declaring an unnecessary type parameter.


There are situations where an additional type parameter would be useful. Imagine for some reason you wanted to return the passed in value:

public <E extends T> E add(E value) {
   ...
   return value;
}

public <E extends T> E add(E value, Node node) {
   ...
   return value;
}

This would now be useful to the caller:

Integer i2 = numTree.add(i);

With the second snippet that wouldn't be possible, and numTree.add could only return a Number even if you passed in an Integer.

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

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