[英]Generic issues: T extends interface type. Cannot call method to return an object which extends the type?
I have this method declared in a parent class: 我在父类中声明了此方法:
protected <ConfigT extends LoadableConfig> ConfigT getConfig(final String configId) {
return (ConfigT)getConfigService().getConfig(configId, getDriver());
}
The configuration service method is defined as follows: 配置服务方法定义如下:
@SuppressWarnings("unchecked")
@Override
public <ConfigT extends LoadableConfig> ConfigT getConfig(String configId, WebDriver driver) {
//noinspection unchecked
Map<String,LoadableConfig> profile = profiles.get(getProfileName(driver));
if(profile != null) {
return (ConfigT) profile.get(configId);
}
return null;
}
The configuration type I want is here: 我想要的配置类型在这里:
public interface AccessibleConfig extends LoadableConfig, PolleableConfig {
....
}
This line of code is throwing an incompatible type error: 这行代码引发了不兼容的类型错误:
AccessibleConfig config = getConfig(ValidationPane.class.getCanonicalName());
How is this possible? 这怎么可能? The
AccessibleConfig
extends LoadableConfig
. AccessibleConfig
扩展了LoadableConfig
。 The method called declared that that return type is some type which extends LoadableConfig
. 称为的方法声明该返回类型是扩展
LoadableConfig
某种类型。 I am running jdk1.8.0_102.jdk. 我正在运行jdk1.8.0_102.jdk。 This failure happens in IntelliJ as well as when compiling with Maven from the command line.
在IntelliJ中以及从命令行使用Maven进行编译时都会发生此故障。 I am able to do this with other configuration types which extends the
LoadableConfig
type. 我可以使用扩展了
LoadableConfig
类型的其他配置类型来执行此操作。
EDIT: 编辑:
Parent class: 家长班:
public abstract class AbstractLoadable<T extends AbstractLoadable<T>> {
private Map<String,LoadableConfig> profiles = new HashMap<>();
protected <T extends LoadableConfig> T getConfig(final String configId) {
return (T) profiles.get(configId);
}
}
Concrete class: 具体课程:
public class ConcreteLoadable extends AbstractLoadable {
public ConcreteLoadable(final String profileName) {
AccessibleConfig config = getConfig(ConcreteLoadable.class.getCanonicalName());
}
}
And the interface types: 以及接口类型:
public interface LoadableConfig {
Integer getLoadTimeoutInSeconds();
void setLoadTimeoutInSeconds(final Integer loadTimeoutInSeconds);
}
public interface AccessibleConfig extends LoadableConfig {
Boolean getHoverOverAccessorWithJavascript();
void setHoverOverAccessorWithJavascript(final Boolean hoverOverAccessorWithJavascript);
Boolean getClickAccessorWithJavascript();
void setClickAccessorWithJavascript(final Boolean clickAccessorWithJavascript);
}
So, the exercise of producing this minimal example actually made it really easy to identify the source of the compiler error. 因此,产生此最小示例的练习实际上使识别编译器错误的根源变得非常容易。 I have posted an answer to this question below.
我在下面发布了这个问题的答案。 I humbly accept that I did not in fact originally post a complete example.
我谦卑地接受我实际上并没有最初发表一个完整的例子。
I have identified the problem. 我发现了问题。 The concrete class needs to pass itself to the parent because the parent is reflexively generic.
具体类需要将自身传递给父类,因为父类是自反通用的。 The error message wasn't super helpful so I got tied up in thinking about the generic parameters of the parent class method and not of the generic parameters applied to the parent class itself.
该错误消息并不是超级有用,因此我被束缚在考虑父类方法的通用参数而不是应用于父类本身的通用参数上。
The class declaration for ConcreteLoadable
needs to changed to this to get rid of the compile error: 需要将
ConcreteLoadable
的类声明更改为此,以摆脱编译错误:
public class ConcreteLoadable extends AbstractLoadable<ConcreteLoadable> {
public ConcreteLoadable(final String profileName) {
AccessibleConfig config = getConfig(ConcreteLoadable.class.getCanonicalName());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.