Java 泛型/接口方法不适用

Java 泛型/接口方法不适用,java,generics,interface,Java,Generics,Interface,我有一个界面蔬菜,例如,苹果,胡萝卜。。。 然后我有一个泛型的类收集器,它们可以是花椰菜、胡萝卜等的收集器。。。 我需要实现一个函数,将一个收集器的carriedObject传递给另一个收集器,问题是如果我尝试了Collector.giveTo(Collector),它无法识别苹果是从蔬菜扩展而来的。 我试过一次,但没有成功 下面是我的类收集器的构造函数: /** define collectors able to collect (and carry) one specific type T

我有一个界面蔬菜,例如,苹果,胡萝卜。。。 然后我有一个泛型的类收集器,它们可以是花椰菜、胡萝卜等的收集器。。。 我需要实现一个函数,将一个收集器的carriedObject传递给另一个收集器,问题是如果我尝试了Collector.giveTo(Collector),它无法识别苹果是从蔬菜扩展而来的。 我试过一次,但没有成功

下面是我的类收集器的构造函数:

/** define collectors able to collect (and carry) one specific type T of objects
 * only one T object can be carried at a time
 */

public class Collector<T> {

private String name;
protected  T carriedObject = null;

/**
 * Creates a Collector object with a name and no carriedObject (carriedObject = null)
 * @param name a String, the name of the Collector
 */
public Collector(String name) {
this.name = name;
this.carriedObject = carriedObject;
}
/**定义能够收集(并携带)一种特定类型T对象的收集器
*一次只能携带一个T形物体
*/
公共类收集器{
私有字符串名称;
受保护的T carriedObject=null;
/**
*创建名为且无carriedObject的收集器对象(carriedObject=null)
*@param name一个字符串,收集器的名称
*/
公共收集器(字符串名称){
this.name=名称;
this.carriedObject=carriedObject;
}
以及使用的某些方法的代码:

/** 
 * give the carriedObject to another Collector
 * @param other the Collector to give the carriedObject to
 * @throws AlreadyCarryingException if the other Collector is already carrying an object
 */
public void giveTo(Collector<T> other) throws AlreadyCarryingException {
    if (this instanceof Vegetable && other instanceof Vegetable){
        if (other.getCarriedObject() != null){
            throw new AlreadyCarryingException("Le collector porte deja un objet");
        }
        else {
            other.take(this.drop());
        }
    }

/**
 * drops the carriedObject setting it to null
 */
public T drop(){
    T tmp = this.carriedObject;
    this.carriedObject = null;
    return tmp;
}

/**
 * allows the Collector to take an object and sets it as his carriedObject
 * @param object the Object to take
 * @throws AlreadyCarryingException if the Collector is already carrying an object
 */
public void take(T object) throws AlreadyCarryingException {
    //Collector is already carrying an object
    if (this.carriedObject != null) {
        throw new AlreadyCarryingException("Le collector porte deja un objet");
    }
    else{
        this.carriedObject = object;
    }
}
/**
*将所携带的对象交给另一个收集器
*@param other要将carriedObject提供给的收集器
*@如果另一个收集器已携带对象,则会引发AlreadyCarringException
*/
public void giveTo(收集器其他)引发AlreadyCarryException{
if(此蔬菜实例和其他蔬菜实例){
if(other.getCarriedObject()!=null){
抛出新的AlreadyCarryException(“Le collector porte deja un objet”);
}
否则{
other.take(this.drop());
}
}
/**
*将carriedObject设置为null,将其丢弃
*/
公共交通{
T tmp=此.carriedObject;
this.carriedObject=null;
返回tmp;
}
/**
*允许收集器获取对象并将其设置为其携带的对象
*@param object要获取的对象
*@如果收集器已携带对象,则会引发AlreadyCarringException
*/
public void take(T对象)抛出AlreadyCarryException{
//收集器已携带一个对象
如果(this.carriedObject!=null){
抛出新的AlreadyCarryException(“Le collector porte deja un objet”);
}
否则{
this.carriedObject=对象;
}
}
例如: 我添加了take和drop的代码

举个例子:

Collector<Carrot> carrotCollector1 = new Collector<Carrot>("carrot-collector-1");
Collector<Vegetable> vegetableCollector = new Collector<Vegetable>("vegetable-collector");
carrotCollector1.giveTo(vegetableCollector);
收集器carrot Collector 1=新收集器(“carrot-Collector-1”);
收集器vegetableCollector=新收集器(“蔬菜收集器”);
胡萝卜采集器1.赠予(蔬菜采集器);
这就是我得到的错误:

类型收集器中给定给(收集器)的方法为 不适用于参数(收集器)


正确的方法是将您的方法更改为:

public void giveTo(Collector<? super T> other) throws AlreadyCarryingException {
   // ... omitted some code  
   other.take(drop());
}

public void giveTo(收集者在您的通话中
carrot collector 1.giveTo(vegetableCollector)
您正在传递类型为
收集器的参数,而它需要类型为
收集器
。@Joakim提供的解决方案应该可以。@RobertKock@Rémy.W np,但是在您说
thx
之前,理解它为什么工作可能很重要。我将把这个留给您阅读,看看
PECS
,以及协方差,然后继续蹂躏。。。