对于这个问题中的任何不良英语,请提前抱歉.
我想到了创建一种工厂,它只能创建由具体工厂实例类实现的列表中定义的类的实例.首先,我定义了这个界面:
public interface ValuesSystem {
public interface AllowedValue>{};
AllowedValue> getAllowedValue(Enum> id);
Map extends Enum>, AllowedValue>> getAllowedValues();
T create(AllowedValue> allowedClass, ValueData data) throws InvalidValueException;
}
AllowedValue接口只是一个“标记包装器”接口,使用泛型来定义我的工厂“允许”创建的Class实例.
getAllowedValue是一个方法,用于从我允许的类“列表”中获取包装的允许类实例,使用实现此接口的具体类的内部枚举(具体类的示例将会出现).
create方法旨在最终创建allowedClass参数中给出的允许的Class实例的实例.
以下是实现此接口的具体类的示例:
public class BasicValueSystem implements ValuesSystem {
public BasicValueSystem() {
super();
allowedValues = (Map>>) getAllowedValues();
}
public static enum VALUES_ID {
MODIFIER
}
private static Map>> allowedValues;
private class BasicAllowedValue>
implements AllowedValue> {
}
@Override
public T create(
AllowedValue> allowedClass, ValueData data)
throws InvalidValueException {
if (!(allowedClass instanceof BasicAllowedValue)) {
throw new InvalidValueException();
}
return null;
}
@Override
public AllowedValue> getAllowedValue(Enum> id) {
return allowedValues.get(id);
}
@Override
public Map extends Enum>, AllowedValue>> getAllowedValues() {
Map>> allowed = new EnumMap>>(VALUES_ID.class);
allowed.put(VALUES_ID.MODIFIER, new BasicAllowedValue>());
return allowed;
}
}
目前,create方法返回null,但问题出在其他地方,这不是我的问题
当我尝试使用以下代码创建一个“允许”值实例的实例时,会发生此问题:
BasicValueSystem bvs = new BasicValueSystem();
AllowedValue> allowed = bvs
.getAllowedValue(BasicValueSystem.VALUES_ID.MODIFIER);
bvs.create(allowed, new ModifierValueData());
编译器告诉我:
The method create(ValuesSystem.AllowedValue>, ValueData) in the type BasicValueSystem is not applicable for the arguments (ValuesSystem.AllowedValue>, ModifierValueData)
我想我错过了泛型一般的类型推断.
任何人都可以解释我创建方法签名在这种情况下不适用以及如何解决它?
提前感谢您抽出宝贵时间.