只需要一些有关此错误的代码帮助:
interface ParticleId{
int getX();
}
class ParticleId1 implements ParticleId{
int a;
@Override
public int getX() {
return a;
}
}
class ParticleId2 implements ParticleId{
int a,b;
@Override
public int getX() {
return a+b;
}
}
interface ParticleInt<K extends ParticleId, O extends ParticleInt<K, O>>{
O withSmt();
}
class Particle<K extends ParticleId> implements ParticleInt<K, Particle<K>>{
@Override
public Particle<K> withSmt() {
return new Particle<>();
}
}
class Particle2<K extends ParticleId> extends Particle<K>{
@Override
public Particle2<K> withSmt() {
return new Particle2<>();
}
}
interface Executable<P extends Particle<? extends ParticleId>>{
void setExecutableStrategy(ExecutableStrategy<P> s);
}
interface ExecutableStrategy<P extends Particle<? extends ParticleId>>{
Stream<P> exec(List<P> l);
}
class Particle2Strat implements ExecutableStrategy<Particle2<? extends ParticleId>>{
@Override
public Stream<Particle2<? extends ParticleId>> exec(List<Particle2<? extends ParticleId>> l) {
return l.stream().map(x -> x.withSmt());
}
}
class ParticleStrat<P extends Particle<? extends ParticleId>> implements ExecutableStrategy<P>{
@Override
public Stream<P> exec(List<P> l) {
return l.stream().map(x -> x.withSmt());
// [44,38] incompatible types: inference variable R has incompatible bounds
// equality constraints: P
// [ERROR] lower bounds: Particle<capture#1 of ? extends ParticleId>
}
}
该错误显示在代码末尾,确切描述为
[44,38] incompatible types: inference variable R has incompatible bounds. equality constraints: P. [ERROR] lower bounds: Particle<capture#1 of ? extends ParticleId>
。我发现有效的快速修复方法是手动将lambda强制转换为(P),例如
(P)x.withSmt()
。如果有可能,有人知道更正确的方法来解决此问题吗?我刚刚更新了代码,因此它不再使用
Integer
而是ParticleId
接口。更新
更新后,我现在遇到这个问题:
class ParticleStrat<P extends ParticleId> implements ExecutableStrategy<Particle<P>>{
@Override
public Stream<Particle<P>> exec(List<Particle<P>> l) {
return l.stream().map(x -> x.withSmt()); //OK NOW
}
}
class MainExecutable<P extends Particle<? extends ParticleId>> implements Executable<P>{
private ExecutableStrategy<P> s;
public MainExecutable() {
this.s = new ParticleStrat<>();
//incompatible types: cannot infer type arguments for ParticleStrat<>
//no instance(s) of type variable(s) P exist so that ParticleStrat<P> conforms to ExecutableStrategy<P>
}
@Override
public void setExecutableStrategy(ExecutableStrategy<P> s) {
this.s = s;
}
}
最佳答案
将ParticleStrat
类的声明更改为以下内容可以解决编译错误:
class ParticleStrat<P extends ParticleInt<P>> implements ExecutableStrategy<P>{
@Override
public Stream<P> exec(List<P> l) {
return l.stream().map(x -> x.withSmt());
}
}
这样,保证
x.withSmt()
返回P
。根据您的问题更改进行编辑:
通过编译:
class ParticleStrat<K extends Integer> implements ExecutableStrategy<Particle<K>>{
@Override
public Stream<Particle<K>> exec(List<Particle<K>> l) {
return l.stream().map(x -> x.withSmt());
}
}
在最新更新之后:
class ParticleStrat<K extends ParticleId> implements ExecutableStrategy<Particle<K>>{
@Override
public Stream<Particle<K>> exec(List<Particle<K>> l) {
return l.stream().map(x -> x.withSmt());
}
}
对于您的最新问题,可以使用相同的方法:
class MainExecutable<K extends ParticleId> implements Executable<Particle<K>>{
private ExecutableStrategy<Particle<K>> s;
public MainExecutable() {
this.s = new ParticleStrat<>();
}
@Override
public void setExecutableStrategy(ExecutableStrategy<Particle<K>> s) {
this.s = s;
}
}