从www.JavaPractices.com复制的代码中<?>
令牌的含义是什么?当我用通用类型的更传统的<T>
替换它时,它不能编译。 (错误:T无法解析为类型。)为什么?Java中<?>标记的含义是什么?
// <?> 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;
}
编辑:当然,当我们使用泛型类型令牌一样<T>
,它必须出现在类声明。在类声明中没有<T>
和<?>
,但它仍然编译并正常运行。
看看我的编辑答案.. – ngesh
检查新的编辑 – BOSS