问题描述
我正在研究一个具有泛型继承和依赖关系树的项目。去编辑看看更好的例子。
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.
Eclipse gives the error of
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.
这篇关于通用限制地狱:绑定不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!