【发布时间】:2020-01-26 14:45:50
【问题描述】:
我的班级中有一个Producer<?, ?> producer 字段,该字段的实现取决于使用构建器模式的给定状态,例如:
private void changeImplementation(int state) {
switch (state) {
case 0:
producer = builder
.setKey(Long.class)
.setValue(String.class)
.setOtherStuff(...)
.build() // return the object with correct key and value
break;
case 1:
...
}
但每当我在生产者上调用方法时(例如使用类型 Producer<Long, String>),都会给出此错误(Eclipse EE):
The method method(Record<capture#9-of ?,capture#10-of ?>) in the type Producer<capture#9-of ?,capture#10-of ?> is not applicable for the arguments (Record<Long,String>)
在build() 之前或在方法调用内部进行强制转换没有帮助。该构建模式在项目的其他地方完美运行。
【问题讨论】:
标签: java eclipse generics design-patterns casting