简体   繁体   English

插入了Enum值的Java泛型?

[英]Java Generics with Enum value inserted?

I have an enum , eg 我有一个enum ,例如

public enum TimerCommandType {
   DATAPUBLISH,
   CLEARCACHE
}

and I want to be able to generate a class using Generics, so it looks like: 并且我希望能够使用泛型来生成一个类,因此它看起来像:

TimerCommandClass<DATAPUBLISH> 

which will use the enum value inside the class when relevant. 相关时将使用类内的enum值。

Is this possible? 这可能吗?

The reason I would like to have it is that I currently have many calls to Quartz Jobs like: 我想拥有它的原因是,我目前有很多对Quartz Jobs的呼叫,例如:

 public JobDetailFactoryBean publishingJobDetail() {
    return QuartzSchedulerHelper.createJobDetail(QuartzPublishingJob.class);
}

and I want them to be replaced to: 我希望将它们替换为:

QuartzSchedulerHelper.createJobDetail(TimerCommandClass<DATAPUBLISH>.class);

where createJobDetail is simply: 其中createJobDetail只是:

static JobDetailFactoryBean createJobDetail(Class jobClass) {
    JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
    factoryBean.setJobClass(jobClass);
    return factoryBean;
}

DATAPUBLISH is a value, not a type, so it can not be used as a type parameter. DATAPUBLISH是一个值,而不是类型,因此不能用作类型参数。

Even if it was possible, type parameters are erased at runtime, so TimerCommandClass<DATAPUBLISH>.class would be the same as TimerCommandClass.class . 即使可能,类型参数也会在运行时删除,因此TimerCommandClass<DATAPUBLISH>.class将与TimerCommandClass.class相同。

TimerCommandClass<DATAPUBLISH> this doesn't make sense it should be parametrized with the class so in our case TimerCommandType not with the instance of the class DATAPUBLISH. TimerCommandClass<DATAPUBLISH>这没有意义,因此应该使用该类进行参数化,因此在我们的情况下,TimerCommandType不能使用类DATAPUBLISH的实例。 To pass the instance you can use the methods or constructor parameters. 要传递实例,可以使用方法或构造函数参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM