通用限制地狱:绑定不匹配 [英] Generic Restriction Hell: Bound Mismatch

查看:99
本文介绍了通用限制地狱:绑定不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个具有泛型继承和依赖关系树的项目。去编辑看看更好的例子。 基本看起来像这样:

  class A {
...
}

class B {
...
}

class C extends B {
...
}

class D< T扩展B>扩展A {
...
}

class StringMap< T extends A> {
HashMap< String,T> _elements;
...
}

现在我要写一个类包含特定的 StringMap 类型。

  class X {
StringMap< D< C>> _thing = new StringMap< D< C>>
...
}

到目前为止,这一切都正常。 D< C> 实际上是一个非常长的名字,并且特定的组合在代码的其他部分会非常频繁地出现,所以我决定针对特定的类因此它会更清晰并且名称更短。

  class DC扩展了D< C> {

}

//并转到更新X
class X {
StringMap< D< C>> _thing = new StringMap< D< C>>(); //仍然正常工作
StringMap< DC> _thing = new StringMap< DC>(); //错误
...
}



Eclipse会给出错误:

lockquote

绑定不匹配: DC 不是对 StringMap< T>类型的有界参数< T extends A> 的有效替代。


所以问题是,为什么这不仅起作用? DC 除了扩展 D< C> 外,并回显构造函数。为什么 StringMap 看到 DC 不同,当它只是一个子类时除外?



编辑:

好​​的,重写了这个例子以更接近我实际做的事情。我测试了它,它确实会产生错误。我在这里做的是使用泛型类型来确保 clone()返回正确的类,以便在继承树中实现它。然后在子类中,我使用 B > 来确保 B的子类正在传递B的子类作为通用类型 T

 公共抽象类可撤消< T>实现可比< T> {
public abstract T clone();
public abstract void updateFields(T modified);
}

abstract public class A< T extends A< T,U>,U extends Comparable< U>>
扩展了可撤销< T> {
abstract U getKey();

@Override
public int compareTo(T element)
{
return getKey()。compareTo(element.getKey());
}
}

public class B< T extends B< T>>扩展A< T,String> {
@Override
public T clone()
{
// TODO自动生成的方法存根
返回null;
}

@Override
public void updateFields(T modified)
{
// TODO自动生成的方法存根
}

@Override
String getKey()
{
// TODO自动生成的方法存根
返回null;
}
}

public class C extends B< C> {

}

公共类D >扩展了A< D< T>,String> {
@Override
String getKey()
{
// TODO自动生成的方法存根
返回null;
}

@Override
public D< T> clone()
{
// TODO自动生成方法存根
返回null;
}

@Override
public void updateFields(D< T>修改过的)
{
// TODO自动生成的方法存根
}
}

public class DC extends D< C> {

}

public class StringMap< T extends Undoable< T>> {
HashMap< String,T> _elements;


$ b $ public class main {
public static void main(String [] args)
{
StringMap< D< C> > _thing = new StringMap< D< C>>(); //作品
StringMap< DC> _thing1 = new StringMap< DC>(); //错误
//绑定不匹配:类型DC不是
的有效替换//绑定参数< T extends Undoable< T>>类型StringMap< T>


$ b


解决方案


$ b

  import java.util.HashMap; 

公共类Q {
class A {
}
class B {
}
class C扩展B {
}
class D< T扩展B>扩展A {
}

class StringMap< T extends A> {
HashMap< String,T> _elements;
}

class DC扩展D< C> {

}

//并转到更新X
class X {
StringMap< D< C>> thing1 = new StringMap< D< C>>(); //仍然正常工作
StringMap< DC> thing2 = new StringMap< DC>(); //没有错误!
}
}

尝试发布这样的课程来重现您的错误。 / p>

I'm working on a project that has an extensive tree of generic inheritance and dependencies. Go to edit to see better example. The basics look something like this:

class A {
  ...
}

class B {
  ...
}

class C extends B {
  ...
}

class D<T extends B> extends A {
  ...
}

class StringMap<T extends A> {
   HashMap<String, T> _elements;
   ...
}

So now I'm going to write a class that contains a specific StringMap type.

class X {
  StringMap<D<C>> _thing = new StringMap<D<C>>;
  ... 
}

So far this all works fine. D<C> is actually a very long name and the specific combination is going to show up very frequently in other parts of the code, so I decided to a class for the specific combination so it will be clearer and have a shorter name.

class DC extends D<C> {

}

//and go to update X
class X {
  StringMap<D<C>> _thing = new StringMap<D<C>>(); //still works fine
  StringMap<DC> _thing = new StringMap<DC>(); //error
  ... 
}

Eclipse gives the error of

Bound mismatch: The type DC is not a valid substitute for the bounded parameter <T extends A> of the type StringMap<T>

So the question is, why does this not just work? DC does nothing but extend D<C> and echo the constructors. Why does StringMap see DC as different when it is just a child class of something it excepts?

EDIT:
OK, reworked the example to be closer to what I'm actually doing. I tested it and it does produce the error. What I'm doing here is using the generic type to ensure that clone() returns the correct class for whoever implements it down the inheritance tree. Then in subclasses, I'm using B<T extends B<T>> to ensure that subclasses of B are passing in a subclass of B as the generic type T.

public abstract class Undoable<T> implements Comparable<T> {
  public abstract T clone();
  public abstract void updateFields(T modified);
}

abstract public class A<T extends A<T, U>, U extends Comparable<U>>
    extends Undoable<T> {
  abstract U getKey();

  @Override
  public int compareTo(T element)
  {
    return getKey().compareTo(element.getKey());
  }
}

public class B<T extends B<T>> extends A<T, String> {
  @Override
  public T clone()
  {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public void updateFields(T modified)
  {
    // TODO Auto-generated method stub
  }

  @Override
  String getKey()
  {
    // TODO Auto-generated method stub
    return null;
  }
}

public class C extends B<C> {

}

public class D<T extends B<T>> extends A<D<T>, String> {
  @Override
  String getKey()
  {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public D<T> clone()
  {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public void updateFields(D<T> modified)
  {
    // TODO Auto-generated method stub
  }
}

public class DC extends D<C> {

}

public class StringMap<T extends Undoable<T>> {
  HashMap<String, T> _elements;

}

public class Main {
  public static void main(String[] args)
  {
    StringMap<D<C>> _thing = new StringMap<D<C>>(); //works
    StringMap<DC> _thing1 = new StringMap<DC>(); //error
//Bound mismatch: The type DC is not a valid substitute for
//the bounded parameter <T extends Undoable<T>> of the type StringMap<T>

  }
}

解决方案

You must be doing wrong something else as the following works fine:

import java.util.HashMap;

public class Q {
    class A {
    }
    class B {
    }
    class C extends B {
    }
    class D<T extends B> extends A {
    }

    class StringMap<T extends A> {
        HashMap<String, T> _elements;
    }

    class DC extends D<C> {

    }

    //and go to update X
    class X {
        StringMap<D<C>> thing1 = new StringMap<D<C>>(); // still works fine
        StringMap<DC> thing2 = new StringMap<DC>(); // NO error!!!
    }
}

Try to post such a class reproducing your error.

这篇关于通用限制地狱:绑定不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆