Java泛型 - 何时使用“T”和“?” [英] Java Generics - When to use "T" and "?"

查看:681
本文介绍了Java泛型 - 何时使用“T”和“?”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别?

  public class MyClass <?扩展MySuperClass> {

私人T数据;
私人MyClass<扩展MySuperClass>父母






  public class MyClass< T extends MySuperClass> {

私人T数据;
//下面的行 - 没有编译错误
私有MyClass<扩展MySuperClass>父母
//下面的行 - 编译错误
private MyCLass< T扩展MySuperClass>孩子


}

何时使用?

解决方案

< T> ,< T> 和<?> 是完全不同的。


    类型参数(占位符)名为 T 的类声明了一个泛型类, 。在该类中, T 是一个有效的类型。 ;某些东西
    是一个变量或字段声明。它声明了一个类型为泛型的变量,指定了泛型参数的值。 ;?>某些东西
    也是一个变量或字段声明。它声明了一个变量,其类型是泛型的,但没有指定泛型参数的值。





创建泛型类时,可以选择向泛型参数添加约束,指定在人们声明该类的实例(变量)时允许使用哪些类型。因此,类X 创建一个具有较少灵活类型参数的泛型类。

/ p>




当您使用泛型类(例如, private Collection< String> myStrings

因此, private Collection< String extends Comparable> myStrings 没有任何意义。






当你使用带有通配符的通用类(或 unbound )参数(例如, private Collection <?> myThingies ),您可以选择指定任何参数可能的约束。这使您可以假设有关泛型参数的(未知)值。

因此,私人收藏<?扩展Runnable> myRunners 是一些未知类型的集合,它保证类型实现 Runnable 。因此,您可以在集合中调用 .run()

但是,您不能在集合中放入任何东西,因为您不知道集合实际上应该容纳什么类型。


What is the difference?

public class MyClass<? extends MySuperClass> {

     private T data ;
     private MyClass<? extends MySuperClass> parent ;

}

and

public class MyClass<T extends MySuperClass> {

     private T data ;
     // the below line - no compilation errors
     private MyClass<? extends MySuperClass> parent ;
     // the below line - compilation errors 
     private MyCLass<T extends MySuperClass> child ;


}

When to use what?

解决方案

<T>, <T>, and <?> are completely different.

  • class X<T> declares a generic class with a type parameter (placeholder) named T. Within that class, T is a valid type.

  • X<T> something is a variable or field declaration. It declares a variable whose type is generic, and specifies the value of the generic parameter.

  • X<?> something is also a variable or field declaration. It declares a variable whose type is generic, but does not specify the value of the generic parameter.


When you create a generic class, you can optionally add constraints to the generic parameter, specifying what types are allowed when people declare instances (variables) of that class.

Thus, class X<T extends SomeClass> creates a generic class with a less-flexible type parameter.


When you use a generic class (eg, private Collection<String> myStrings), you obviously cannot specify constraints on the value of the generic parameter (since you're already specifying the actual value).

Thus, private Collection<String extends Comparable> myStrings makes no sense.


When you use a generic class with a wildcard (or unbound) parameter (eg, private Collection<?> myThingies), you can optionally specify constraints on the whatever the parameter might be. This allows you to assume things about the (unknown) value of the generic parameter.

Thus, private Collection<? extends Runnable> myRunners is a collection of some unknown type that guarantees that the type implements Runnable. Therefore, you can call .run() on the things in the collection.
However, you can't put anything in the collection, since you don't know what type the collection is actually supposed to hold.

这篇关于Java泛型 - 何时使用“T”和“?”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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