<>的含义是什么? Java中的令牌?

编程入门 行业动态 更新时间:2024-10-25 14:27:29
本文介绍了<>的含义是什么? Java中的令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

从www.JavaPractices复制的代码中<?> 标记的含义是什么?当我将它替换为用于泛型的更传统的< 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

  • 使StopAlarmTask成为泛型(在这种情况下没有意义)
  • 在ScheduledFuture中使用具体类型,但它将只有一种可能的结果类型,例如String或Integer
  • 使用通配符(< ;?> ) - 它允许检索任何作为FutureResult(String,Integer,您的自定义类)的结果。您还可以将可能的泛型类型的范围缩小为某些子类,例如 ScheduledGeneric< ?扩展MyObject> 或扩展为超类: ScheduledGeneric< ?超级MyObject>
  • What is the meaning of the <?> token in this code copied from www.JavaPractices? 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:

  • make StopAlarmTask generic (there is no sense in this case)
  • use concrete type in ScheduledFuture, but then it would be only one possible result type, for example String or Integer
  • 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中的令牌?

    本文发布于:2023-08-01 09:24:41,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1267235.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:令牌   含义   lt   amp   Java

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!