これはおそらく以前に何らかの形で尋ねられたことがありますが、まだ解決できないので、ここで集合的な知恵を求めようと思いました。私はそのようなインターフェースを持っています - 役に立つかもしれないので、いくつかのコメントを残しました。
/** Technical note: this generic binding is known as F-bound where class
* T is 'the worked-on class' by Splittable i.e. the implementing class.
*/
interface Splittable <T extends Element & Splittable<T>> {
/** Returns an object of the same class which has been split off at
* the point given.
*/
public T splitOff(double at);
/** Appends the object provided, which must be of the same class, to
* this object.
*/
public void append(T el);
}
したがって、コンパイル時にわからない要素に取り組んでいます。
if (el instanceof Splittable) {
if (nextEl.getClass == el.getClass) {
// same class so join them
((Splittable)el).append(nextEl);
}
}
そして、それがコンパイラの警告を受け取る場所です-生の型Splittableのメンバーとしてのappend(T)へのチェックされていない呼び出し。
Element サブクラスでは、両方を試しました
SubClassEl extends Element implements Splittable
と
SubClassEl extends Element implements Splittable<SubClassEl>
警告に違いはありません。
また、append() を呼び出す時点で、私は試しました
((Splittable)el).splitOff(x).append(nextElement)
ただし、コンパイラは、splitOff が Splittable を返す必要があることを認識せず、Element のみを返します。
何か案は?