9

2つのオブジェクトをパラメーターとして取るfooというメソッドがあるとします。両方のオブジェクトは同じタイプであり、どちらも同等のインターフェースを実装しています。

void foo(Object first, Object second){

    if (!first.getClass().isInstance(second))   //first and second of the same type
        return;

    Comparable firstComparable = (Comparable)first;  //WARNING
    Comparable secondComparable = (Comparable)second;  //WARNING

    int diff = firstComparable.compareTo(secondComparable);  //WARNING
}

最初の2つの警告は次のとおりです。

Comparableはrawタイプです。ジェネリック型Comparableへの参照はパラメーター化する必要があります

最後の警告:

型安全性:メソッドcompareTo(Object)は、raw型Comparableに属しています。ジェネリック型Comparableへの参照はパラメーター化する必要があります

これらの警告を削除するために、コードをリファクタリングするにはどうすればよいですか?

編集:fooメソッドの署名を変更せずにそれを行うことはできますか?

4

5 に答える 5

15

それらが同じタイプで比較可能であることをコンパイラーに通知する必要があります。シグニチャを変更できない場合は、下位互換性のためのメソッドを追加できます。

@SuppressWarnings("unchecked")
static void foo(Object first, Object second) {
    foo((Comparable) first, (Comparable) second);
}

static <T extends Comparable<T>> void foo(T first, T second){
    int diff = first.compareTo(second); // no warning.
}
于 2011-08-10T09:48:36.227 に答える
3

署名を変更せずに行うことができます

    void foo(Object first, Object second){

        if (!first.getClass().isInstance(second)) 
            return;

        Comparable<Object> firstComparable = (Comparable<Object>)first;  
        Comparable<Object> secondComparable = (Comparable<Object>)second; 

        int diff = firstComparable.compareTo(secondComparable);  
    }

しかし、あなたはまだ得ました:
Type safety: Unchecked cast from Object to Comparable<Object>

Comparable is a raw type. References to generic type Comparable<T> should be parameterized
でもダメだType safety: The method compareTo(Object) belongs to the raw type Comparable. References to generic type Comparable<T> should be parameterized

于 2011-08-10T09:59:38.027 に答える
1

Comparable<Type>Typeが実装しているオブジェクトである場合に使用する必要が ありますComparable

まず、メソッドパラメータがインスタンスであるのはなぜObjectsですか?パラメータのタイプが同じであることが確実な場合は、特定のクラスをパラメータとして使用する必要があります。クラスの階層を持つことができる場合は、階層の最上位にクラスを配置します。一般的な機能をObject実現することは決して良い考えではありません。

于 2011-08-10T09:51:04.423 に答える
1

編集:メソッドのシグネチャを変更することはできないと言ったので、(コンパイラにとって)安全でないキャストと@SuppressWarnings:なしでは本当に逃げることはできません。

@SuppressWarnings("unchecked")
public void foo(final Object first, final Object second) {
    if (!first.getClass().isInstance(second)) // first and second of the
        return;

    Comparable<Object> firstComparable = (Comparable<Object>) first;
    Comparable<Object> secondComparable = (Comparable<Object>) second;
    int diff = firstComparable.compareTo(secondComparable);
}
于 2011-08-10T10:01:58.453 に答える
0

@SuppressWarningsアノテーションを追加します。

@SuppressWarnings("unchecked")
void foo(Object first, Object second){

    if (!first.getClass().isInstance(second))   //first and second of the same type
        return;

    Comparable firstComparable = (Comparable)first;  //WARNING
    Comparable secondComparable = (Comparable)second;  //WARNING

    @SuppressWarnings("unused")
    int diff = firstComparable.compareTo(secondComparable);  //WARNING
}
于 2011-08-10T09:45:23.640 に答える