如何实现已参数化的接口并扩展其他两个接口?

时间:2016-02-29 08:48:12

标签: java interface

我有三个接口:

public interface IAggregable<TElement extends IAggregable<TElement, TResult>, TResult> {
    TResult aggregate(TResult intermediateResult);
}

public interface IDeeplyCloneable<TElement extends IDeeplyCloneable<TElement>> {
    TElement deepClone();
}

public interface IContainer<TElement extends IAggregable<TElement, TResult> & IDeeplyCloneable<TElement>, TResult> {
    TResult aggregateAllElements();
    TElement cloneElementAtIndex(int index);
}

我尝试实现最后一个;

public class GTZCH<TElement, TResult> implements IContainer<TElement, TResult> {

    public GTZCH() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public TResult aggregateAllElements() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public TElement cloneElementAtIndex(int index) {
        // TODO Auto-generated method stub
        return null;
    }

}

但是我收到了编译错误:

Bound mismatch: The type TElement is not a valid substitute for the bounded parameter <TElement extends 
 IAggregable<TElement,TResult> & IDeeplyCloneable<TElement>> of the type IContainer<TElement,TResult>

&运算符在exends部分中意味着什么,我认为应该有一个昏迷不是&amp;扩展多个接口。

如何以我不必使用原始类型的方式实现该接口?

1 个答案:

答案 0 :(得分:0)

是的,你应该使用&amp;在类型约束中,在声明GTZCH时定义TElement的界限:

public class GTZCH<TElement extends IAggregable<TElement,TResult> & IDeeplyCloneable<TElement>, TResult> implements IContainer<TElement, TResult> {
    [...]
}