Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java泛型和ApacheOlingo4编译错误_Java_Generics_Olingo - Fatal编程技术网

使用Java泛型和ApacheOlingo4编译错误

使用Java泛型和ApacheOlingo4编译错误,java,generics,olingo,Java,Generics,Olingo,我已经扩展了的一些基类(仍在开发中),以允许更强的类型。然而,我对泛型的使用导致了一个我没有预料到的错误 我有一个扩展FoodataEntity的类型参数E,它反过来实现了ODataEntity接口。由于FooODataEntity是一个ODataEntity(只是更具体),我希望编译时不会出现任何问题。但是,getEntities()有一个编译错误,如下代码所示 另外,我希望能够将List指定为重写getEntities()的返回类型,但随后会出现一个编译错误: 'com.foo.restap

我已经扩展了的一些基类(仍在开发中),以允许更强的类型。然而,我对泛型的使用导致了一个我没有预料到的错误

我有一个扩展FoodataEntity的类型参数E,它反过来实现了ODataEntity接口。由于FooODataEntity是一个ODataEntity(只是更具体),我希望编译时不会出现任何问题。但是,getEntities()有一个编译错误,如下代码所示

另外,我希望能够将
List
指定为重写
getEntities()
的返回类型,但随后会出现一个编译错误:

'com.foo.restapi.client.olingo.footentityset'中的'getEntities()'与'org.apache.olingo.commons.api.domain.v4.ODataEntitySet'中的'getEntities()'冲突;试图使用不兼容的返回类型

我错过了什么

FooODataEntitySet:

package com.foo.restapi.client.olingo;

import com.foo.restapi.client.FooODataEntity;
import com.foo.restapi.client.exceptions.FooRuntimeException;

import org.apache.olingo.commons.api.domain.v4.ODataAnnotation;
import org.apache.olingo.commons.api.domain.v4.ODataEntity;
import org.apache.olingo.commons.api.domain.v4.ODataEntitySet;
import org.apache.olingo.commons.core.domain.AbstractODataEntitySet;

import java.lang.reflect.Constructor;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

public class FooEntitySet<E extends FooODataEntity> 
        extends AbstractODataEntitySet implements ODataEntitySet {

    private final List<E> entities = new ArrayList<E>();

    public FooEntitySet() {
        super();
    }

    @Override
    public List<ODataEntity> getEntities() {
        // compile error  
        // Incompatible types. Found: 'java.util.List<E>', 
        // required: 'java.util.List<org.apache.olingo.commons.api.domain.v4.ODataEntity>'

        return entities;
    }
}

你不能这么做是有原因的。虽然
foodataentity
ODataEntity
,但
列表
不是a
列表

让我们更详细地介绍一下:

假设我有这门课:

public class BaconODataEntity implements ODataEntity {
    // Pretend I implement all the ODataEntity things
}
我可以将
BaconODataEntity
的实例添加到
列表和
列表中。。。但不在
列表中

因此,简单地让您将
列表
强制转换为
列表
将破坏泛型要引入的类型安全性,因为我可以在其中添加
BaconODataEntity


那么,你如何修复它呢?好吧,如果你确实需要你的列表成为一个
列表
,并返回该列表。

你能同时包含从类中导入的内容吗?@Qix请参阅更新的答案。
fooodateentity
呢?如果你没有进口,那就是你的问题。确保
foodataentity
的源文件包含
ODataEntity
ODataSingleton
的导入。导入很好,问题是根据Powerlord下面的回答对转换泛型产生了误解。感谢这个示例,它显示了我的想法是错误的。我确实需要该列表来扩展
foodataentity
以获得另一种方法,因此我按照您的建议复制到了一个新的
ODataEntity
类型的列表中。是的,我肯定没有想得太多。:)
public class BaconODataEntity implements ODataEntity {
    // Pretend I implement all the ODataEntity things
}