<>的含义是什么? Java中的令牌? [英] What is the meaning of the <?> token in Java?

查看:156
本文介绍了<>的含义是什么? Java中的令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从www.JavaPractices.com复制的代码中<?> 标记的含义是什么?当我将它替换为用于泛型的更传统的< T> 时,它将无法编译。 (错误:T无法解析为类型。)为什么?

  //<?>在整个程序中发生3次。当它被< T>替换时, 
//程序不再编译。

void activateAlarmThenStop()
{
Runnable myPeriodicTask = new PeriodicTask();
ScheduledFuture<?> soundAlarmFuture =
this.executorService.scheduleWithFixedDelay(myPeriodicTask,
startT,
period,
TimeUnit.SECONDS
);
Runnable stopAlarm = new StopAlarmTask(soundAlarmFuture);
this.executorService.schedule(stopAlarm,stopT,TimeUnit.SECONDS);


private final class StopAlarmTask implements Runnable
{
StopAlarmTask(ScheduledFuture<> aSchedFuture)
{
fSchedFuture = aSchedFuture;


public void run()
{
CConsole.pw.println(Stopping alarm。);
fSchedFuture.cancel(doNotInterruptIfRunningFlag);

executorService.shutdown();
}
private ScheduledFuture<?> fSchedFuture;编辑:当然,当我们使用泛型类型的标记,如< T> ,它必须出现在类声明中。这里在类声明中没有< T> <?> ,但它仍然编译并运行正确。

解决方案

它无法编译,因为你的类不是泛型的(也不是你的任何方法)。在这个特殊的例子中,小丑(?)意味着ScheduledFuture可以被任何参数化。

有时候,如果在内部使用另一个泛型类,并且不知道将使用的确切类型,则无法使整个类变为泛型。
在这个例子中,您有三个选项:
$ b


  1. 使StopAlarmTask成为泛型(在这种情况下没有意义)

  2. 在ScheduledFuture中使用具体类型,但它将只有一种可能的结果类型,例如String或Integer

  3. 使用通配符(< ;?> ) - 它允许检索任何作为FutureResult(String,Integer,您的自定义类)的结果。您还可以将可能的泛型类型的范围缩小为某些子类,例如 ScheduledGeneric< ?扩展MyObject> 或扩展为超类: ScheduledGeneric< ?超级MyObject>


What is the meaning of the <?> token in this code copied from www.JavaPractices.com? When I replace it with the more conventional looking <T> used for generic types, it fails to compile. (Error: T cannot be resolved to a type.) Why?

// <?> occurs 3 times in the entire program.  When it is replaced with <T> the
// program no longer compiles.

void activateAlarmThenStop()
{
    Runnable myPeriodicTask = new PeriodicTask();
    ScheduledFuture<?> soundAlarmFuture = 
        this.executorService.scheduleWithFixedDelay(myPeriodicTask, 
                                          startT, 
                                          period, 
                                          TimeUnit.SECONDS
                                         );
    Runnable stopAlarm = new StopAlarmTask(soundAlarmFuture);
    this.executorService.schedule(stopAlarm, stopT, TimeUnit.SECONDS);
}

private final class StopAlarmTask implements Runnable 
{
    StopAlarmTask(ScheduledFuture<?> aSchedFuture)
    {
        fSchedFuture = aSchedFuture;
    }

    public void run() 
    {
        CConsole.pw.println("Stopping alarm.");
        fSchedFuture.cancel(doNotInterruptIfRunningFlag);

        executorService.shutdown();
    }
    private ScheduledFuture<?> fSchedFuture;
}

Edit: Of course when we use generic type tokens like <T>, it has to appear in the class declaration. Here there is no <T> nor <?> in the class declaration but it still compiles and runs properly.

解决方案

It fails to compile, because your class is not generic (nor any of your methods). In this particular example joker (?) means that ScheduledFuture may be parametrized by anything.

Sometimes, there is no sense to make the whole class generic if you use another generic class inside and you don't know the exact type that will be used. In this example you had three options:

  1. make StopAlarmTask generic (there is no sense in this case)
  2. use concrete type in ScheduledFuture, but then it would be only one possible result type, for example String or Integer
  3. use wildcard (< ? >) - it allows to retrieve anything as a result of FutureResult (String, Integer, your custom class). You can also narrow the scope of a possible generic type into some subclasses, for example ScheduledGeneric< ? extends MyObject > or into superclasses: ScheduledGeneric< ? super MyObject >

这篇关于&lt;&gt;的含义是什么? Java中的令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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