Java泛型 - 不兼容的类型

时间:2017-07-27 08:37:46

标签: java generics

在代码中只需要一些关于此错误的帮助:

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;
    }
}

1 个答案:

答案 0 :(得分:1)

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;
    }
}