&lt; T延伸AnInterface&gt; vs <?扩展AnInterface&gt; [英] &lt;T extends AnInterface&gt; vs &lt;? extends AnInterface&gt;

查看:120
本文介绍了&lt; T延伸AnInterface&gt; vs <?扩展AnInterface&gt;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个不是集合的类,但它确实引用了泛型对象:

  public class XClass< E extends AnInterface> {

E instanceobject;
$ b $ public void add(E toAdd){}
}

public interface AnInterface {}

public class A implements AnInterface {}

public class B implements AnInterface {}

我相信我在某处读过< ;?如果您希望同时在通用对象中使用多个子类型,而使用< T扩展AnInterface>,则将使用扩展AnInterface> (在声明XClass的实例时) ; 只允许您在泛型类中同时拥有一种类型的子类型?



但是,我可以使用:

  XClass< AnInterface> xc = new XClass< AnInterface>(); 

A a = new A();
B b = new B();

xc.add(a);
xc.add(b);

这样我就可以将Supertype的多个子类传递给泛型类......



我没有看到使用?的目的并使用接口作为泛型参数有什么问题吗?

解决方案

您可以添加类型 A B 是由于您使用接口参数化了您的XClass,所以添加两个元素没有任何问题不同的类实现该接口。



另一方面,如果您已将XClass定义为:

  XClass< a取代; xc = new XClass< A>(); 

然后表达式 xc.add(b); 会产生编译错误,因为所有添加的对象都必须与声明的类型相同,在这种情况下,A。



如果声明< xc as,例如:



XClass <?扩展AnInterface> xc = new XClass< AnInterface>();



然后添加 a b ,因为我们唯一知道的是xc有一些未知的但固定的子类型 AnInterface ,并且无法知道未知类型是 A 还是 B 或其他任何东西。



但是让我们假设你正在编写一个方法来接受一个XClass类型,你可以迭代之前添加的元素。您唯一的限制(为了举例)是,这些项目扩展 AnInterface ,您不关心实际类型。



您可以声明此方法,如:

  public static void dummyMethod(XClass< ;? extends AnInterface> dummy){
//在这里做的东西,所有的元素扩展(在这种情况下实现),AnInterface,疯狂。
}

现在你可以传入这个方法,就像 XClass< A> XClass< B> XClass< AnInterface> 都将是有效的。



请记住,您不能添加到您传递的对象,出于上述相同的原因。我们不知道未知类型是什么!

  public static void dummyMethod(XClass< ;? extends AnInterface> dummy){
//在这里做些东西,所有元素都会扩展(在这种情况下实现),AnInterface会变得疯狂。
dummy.add(new A()); //你不能这样做,我们不知道什么类型?在这种情况下代表
}


I am a little confused with something.

I have a class where its not a collection, but it does refer to generic objects:

    public class XClass<E extends AnInterface>{

        E instanceobject;

        public void add(E toAdd){}
    }

    public interface AnInterface{}

    public class A implements AnInterface{}

    public class B implements AnInterface{}

I believe I read somewhere that <? extends AnInterface> is to be used (when declaring an instance of XClass) if you want multiple subtype-types in the generic object at the same time, whereas <T extends AnInterface> would only allow you to have a single type of subtype in the generic class at once?

However, I can just use:

    XClass<AnInterface> xc = new XClass<AnInterface>();

    A a = new A();
    B b = new B();

    xc.add(a);
    xc.add(b);

and this way I can pass in multiple subtypes of Supertype to the generic class......

I am not seeing the purpose of using "?" and is there anything wrong with using the Interface as the generic parameter?

解决方案

The reason why you can add objects of both type A and B is due to the fact that you parametized your XClass with the interface, so there is nothing wrong with adding two different classes that implement that interface.

If, on the other hand, you had defined XClass as:

XClass<A> xc = new XClass<A>();

then the expression xc.add(b); would give a compilation error, since all the objects added must have the same type as was declared, in this case, A.

If you declare you xc as, for instance:

XClass<? extends AnInterface> xc = new XClass<AnInterface>();

Then it's not legal anymore to add a or b, since the only thing we know is that xc is of some unknown but fixed subtype of AnInterface, and there is no way to know if that unknown type is A or B or anything else.

But let's say you're writing a method to accept a XClass type that you can iterate over the elements that were added before. Your only restriction (for the sake of the example), is that the items extend AnInterface, you don't care what the actual type is.

You can declare this method like:

public static void dummyMethod(XClass<? extends AnInterface> dummy){
//do stuff here, all the elements extend (implement in this case), AnInterface, go wild.
}

And now you can pass into this method anything like XClass<A>, XClass<B> or XClass<AnInterface>, and it will all be valid.

Keep in mind that you can't add to the object you pass, for the same reason above. We don't know what the unknown type is!

public static void dummyMethod(XClass<? extends AnInterface> dummy){
//do stuff here, all the elements extend (implement in this case), AnInterface, go wild.
    dummy.add(new A()); //you can't do this, we have no idea what type ? stand for in this case
}

这篇关于&lt; T延伸AnInterface&gt; vs <?扩展AnInterface&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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