gpt4 book ai didi

Java 泛型 - 不兼容的类型

转载 作者:行者123 更新时间:2023-11-30 10:27:19 25 4
gpt4 key购买 nike

只需要一些帮助来解决代码中的这个错误:

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

关于Java 泛型 - 不兼容的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45345495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com