java界面泛型铸造问题 [英] java interface generics casting problem

查看:81
本文介绍了java界面泛型铸造问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方法add(捕获#2-of?extends IObject)在List类型中不适用于参数(IDerived)

 受保护的列表<?扩展IObject> getObjects()
{
List< ;?扩展IObject> objects = new ArrayList< IObject>();
for(String id:item_ids)
{
IDerived object =(IDerived)readObject(id);
objects.add(object); #error
}
返回对象;
}

接口IDerived扩展接口IVersionedObject扩展接口IObject

如果我将对象的类型更改为List,那么错误消失,这是没有意义的,因为它必须对函数返回类型进行完全相同的转换。

解决方案

试试这个:

  protected< T extends IObject>列表与LT; T> getObjects(){
List< T> objects = new ArrayList< T>();
for(String id:item_ids)
{
T object =(T)readObject(id);
objects.add(object); //无错误
}
返回对象;





编辑(简短解释):

这样,你的方法可以很好地处理IObject的任何子类型(列表和内部变量是通用的),并且返回类型将根据调用者的期望推断出来(如你明显想要的那样)。



在回应评论时,您现在可以通过以下方式调用getObjects()方法:

  //这应该只在readObject()行为像
// getObjects()并且能够返回任何请求的子类型时使用。
//否则,如果在readObject()中放入
// else there(它不是子类型IDerived经)。
列表< IDerived> derivedList = getObjects();

//如果您没有
//完全控制readObject()返回的内容,这是安全的方法。
//但是如果你像这样使用它(所有的时候),你最好
// return List< IObject>从getObjects()中去掉
//泛型。
列表< ;?扩展IObject> objectList1 = getObjects();
列表< IObject> objectList2 = getObjects();


The method add(capture#2-of ? extends IObject) in the type List is not applicable for the arguments (IDerived)

protected List<? extends IObject> getObjects()
{
    List<? extends IObject> objects = new ArrayList<IObject>();
    for (String id: item_ids)
    {
        IDerived object = (IDerived) readObject(id);
        objects.add(object); #error
    }
    return objects;
}

interface IDerived extends interface IVersionedObject extends interface IObject

If I change the type of objects to List then the error goes away, which makes no sense because it has to make exactly the same cast to the function return type.

解决方案

Try this:

protected <T extends IObject> List<T> getObjects() {
    List<T> objects = new ArrayList<T>();
    for (String id: item_ids)
    {
        T object = (T) readObject(id);
        objects.add(object); // NO error
    }
    return objects;
}

EDIT (short explanation):

This way your method will work fine with any subtype of IObject (the lists and casts inside get to be generic) and the return type will be inferred from the caller's expectations (as you apparently intended).

In response to the comment, you can now call the getObjects() method in the following ways:

// This should only be used if readObject() behaves like
// getObjects() and is able to return any requested subtype.
// Otherwise, you'll get a ClassCastException when trying to get
// something from the derivedList in case readObject() put something
// else there (which is not a subtype of IDerived).
List<IDerived> derivedList = getObjects();

// This is the safe way to go in case you don't have
// full control over what readObject() returns.
// But if you're using it like this (all the time), you'd better
// return List<IObject> from getObjects() and get rid
// of generics.
List<? extends IObject> objectList1 = getObjects();
List<IObject> objectList2 = getObjects();

这篇关于java界面泛型铸造问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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