java中的通配符通用和&lt ;?超T>意思是下限或上限 [英] Wild card in java Generic and <? super T> meaning, lower or upper bound

查看:123
本文介绍了java中的通配符通用和&lt ;?超T>意思是下限或上限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在阅读关于泛型方法,我感到困惑。首先让我在这里陈述问题:在这个例子中:假设我需要一个适用于任何类型T的selectionSort版本,通过使用外部可比较的

第一次尝试:

  public static< T> void selectionSort(T [] arr,Comparator< T> myComparator){....} 

假设我有:


  • 定义的车辆类

  • 创建VehicleComparator实现Comparator,而
    比较车辆的价格。
  • 创建卡车延伸车辆

  • 实例化卡车[] arr; VehicleComparator myComparator


现在,我确实:

  selectionSort(arr,myComparator); 

并且它不起作用,因为myComparator不适用于Vehicle的任何子类。

然后,我这样做:

  public static< T> ; void selectionSort(T [] arr,Comparator<?super T> myComparator){....} 

这个声明会起作用,但我不完全确定我一直在做什么......我知道使用是要走的路。如果超级T是指一个未知的超类型T,那么我是施加一个上限还是下限?为什么它超级?我的意图是让T的任何子类使用myComparator,为什么?super T。所以很困惑...我会很感激,如果你有这方面的任何洞察力。



感谢您的提前!

解决方案

首先,您可以通过 Vehicle [] 来解决问题,然后添加 Truck s。



您需要<的原因超级T> 返回到泛型规则: Comparator< Truck> 不是的比较类型< Vehicle> / code>;无界类型 T 必须完全匹配,否则它不会。



为了获得合适的 Comparator 被传入,它必须是被比较的类的一个 Comparator 或它的任何超类,因为在OO语言中任何类都可以被视为超类的实例。因此,只要它是数组元素的超类型, Comparator 的泛型类型就无关紧要。


So I am reading about generic method and I am get confused. Let me state the problem here first:

In this example: Suppose that I need a version of selectionSort that works for any type T, by using an external comparable supplied by the caller.

First attempt:

public static <T> void selectionSort(T[] arr, Comparator<T> myComparator){....}

Suppose that I have:

  • Defined vehicle class
  • created VehicleComparator implementing Comparator while compare vehicles by their price.
  • created Truck extends vehicle
  • instantiated Truck[] arr ; VehicleComparator myComparator

Now, I do:

selectionSort(arr, myComparator);

and it won't work, because myComparator is not available for any subclass of Vehicle.

Then, I do this:

public static <T> void selectionSort(T[] arr, Comparator<? super T> myComparator){....}

This declaration will work, but I don't completely sure what I've been doing... I know use is the way to go. If "? super T" means "an unknown supertype of T", then am I imposing a upper or lower bound? Why is it super? My intention is to let any subclass of T to use myComparator, why "? super T". So confused... I'd appreciate if you have any insight in this..

Thanks ahead!

解决方案

Firstly, you could have solved it by having Vehicle[] which you then added Trucks to.

The reason you need <? super T> goes back to the generics rule that Comparator<Truck> is not a subtype of Comparator<Vehicle>; the unbounded type T must match exactly, which it doesn't.

In order for a suitable Comparator to be passed in, it must be a Comparator of the class being compared or any super class of it, because in OO languages any class may be treated as an instance of a superclass. Thus, it doesn't matter what the generic type of the Comparator is, as long as it's a supertype of the array's component type.

这篇关于java中的通配符通用和&lt ;?超T&gt;意思是下限或上限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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