“尝试使用不兼容的返回类型";与实现Serializable的类 [英] "Attempting to use incompatible return type" with class implementing Serializable
问题描述
我有以下界面:
切片器
public interface Slicer {
Optional<Map<String, ? extends Serializable>> pointer();
}
我有一个实现:
DynamoDbSlicer
public abstract class DynamoDbSlicer implements Slicer {
@Override
public abstract Optional<Map<String, AttributeValue> pointer();
}
其中AttributeValue
来自AWS开发工具包,并定义为:
where AttributeValue
is from the AWS SDK and defined as:
public final class AttributeValue implements SdkPojo, Serializable, ToCopyableBuilder<AttributeValue.Builder, AttributeValue> {
请注意,它实现了Serializable
.
我在DynamoDbSlicer
中的抽象方法上遇到编译器错误:
I am getting a compiler error on the abstract method in DynamoDbSlicer
:
pointer() in DynamoDbSlicer clashes with pointer() in Slicer; attempting to use incompatible return type
我想念什么?
推荐答案
乍一看,这似乎应该编译,因为Java(自1.5开始)具有返回类型协方差.这意味着重写方法可以声明一个返回类型,该类型是原始方法的返回类型的子类型.
At first glance, this looks like it should compile, because Java (since 1.5) has had return type covariance. This means that an overriding method can declare a return type that is a subtype of the original method's return type.
看起来Optional<Map<String, Serializable>>
是Optional<Map<String, ? extends Serializable>>
的子类型,但不是.有趣的是,如果您从两种返回类型中都删除了Optional
部分,则会进行编译.
It looks like Optional<Map<String, Serializable>>
is a subtype of Optional<Map<String, ? extends Serializable>>
, but it's not. Interestingly, if you remove the Optional
part from both return types, this compiles.
interface Slicer {
Map<String, ? extends Serializable> pointer();
}
abstract class DynamoDbSlicer implements Slicer {
@Override
public abstract Map<String, Serializable> pointer();
}
之所以进行编译,是因为Map<String, Serializable>
是Map<String, ? extends Serializable>
的子类型-可以将前者的实例分配给后者的变量.
It compiles because Map<String, Serializable>
is a subtype of Map<String, ? extends Serializable>
-- one can assign an instance of the former to a variable of the latter type.
However, adding Optional
makes it fail to compile for the same reason that a List<Dog>
isn't a List<Animal>
-- generics are invariant. In this analogy, Dog
is like the specific subtype that matches Map<String, Serializable>
, and Animal
is like the general type that matches Map<String, ? extends Serializable>
. Just like a List<Dog>
is not a List<Animal>
, a Optional<Map<String, Serializable>>
is not a Optional<Map<String, ? extends Serializable>>
.
在不删除Optional
位的情况下进行编译的最简单方法是完全匹配类型.
The easiest way of getting this to compile without removing the Optional
bit is to match the type exactly.
abstract class DynamoDbSlicer implements Slicer {
@Override
public abstract Map<String, ? extends Serializable> pointer();
}
如果这不符合您的要求,那么您需要在您的类可以作为类型实参提供的接口上创建一个类型实参.
If this doesn't fit your requirements, then you'll need to create a type parameter on the interface that your class can supply as a type argument.
interface Slicer<T extends Serializable> {
Optional<Map<String, T>> pointer();
}
abstract class DynamoDbSlicer implements Slicer<AttributeValue> {
@Override
public abstract Optional<Map<String, AttributeValue>> pointer();
}
type参数允许它进行编译.
The type parameter allows this to compile.
这篇关于“尝试使用不兼容的返回类型";与实现Serializable的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!