2016-02-29 46 views
0

我有三個接口:如何實現已經參數化並擴展了兩個其他接口的接口?

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部分是指,我認爲應該有一個昏迷不&擴展多個接口。

如何實現該接口的方式我不會使用原始類型?

+0

這是'型和一個'約束。請參閱[關於類型邊界的教程](https://docs.oracle.com/javase/tutorial/java/generics/bounded.html)。 –

回答

0

是的,你應該在類型限制使用&並定義聲明GTZCH當您綁定的TElement:

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