2017-11-02 9 views
1

は、次のコードを考えてみてください:2つのオブジェクトが同じだが不明なクラスであることをコンパイラに伝えるにはどうすればよいですか?

public class MyClass { 

    public static void main(String[] args) {  
    Object o1 = getObject(Math.random()); 
    Object o2 = getObject(Math.random()); 
    if (o1.getClass().equals(o2.getClass()) { // two Cars or two Apples 
     Comparable c1 = (Comparable) o1; 
     Comparable c2 = (Comparable) o2; 
     int x = c1.compareTo(c2); // unsafe 
     System.out.println(x); 
    ) 
    } 

    public Object getObject(double d) { // given method that may not be changed 
    if (d < 0.5) return (new Car()); // Car implements Comparable<Car> 
    else return (new Apple()); // Apple implements Comparable<Apple> 
    } 

} 

(クラスCatApple与えられた)コードは動作しますが、私はジェネリックなしcompareToを使用しているため、コンパイラは、安全でない操作のために警告します。しかし、c1c2が同じであるが不明なタイプ(if句)であることを指定する方法がわからないため、これを修正する方法はわかりません。これを修正する方法はありますか(もちろん@SuppressWarningsを使用しています)?

私はここに似たような質問があることを知っています:How to tell Java that two wildcard types are the same? しかし答えは質問者の文脈に固有のようです。たとえば、自分のコンテキストに存在しないキー値マップを使用します。

答えて

-2

instanceof演算子を使用できます。

if(c1 instanceof Integer && c2 instanceof Integer) 
    int x = c.compareTo(c2); 





or if(c1 instanceof String) 
    String s = c1; 

このように書くことができます。

+0

それは一例だと、あなたは、このコードを見れば、彼はラッパークラスInteger型のインスタンスであり、彼がこれを理解している場合、彼は他のチェックoffcouseできるint型を比較していますインスタンスも同様です。これは彼のタイプをチェックせずにintに値をassinしようとしていたので、彼の警告を削除する必要があります。 –

+0

はい私はプロローグbtwに何かを書いていました。ありがとう、私はそれを編集します。 –

1

条件if (o1.getClass().equals(o2.getClass()))は、2つのオブジェクトが同じクラスであることを保証するため、警告を無視しても安全です。 あなたはそれを抑制することができます。

ただし、タイプがObjectなので、 Comparableにキャストしても安全ではありません。 あなたはクラスComparableのそれらを作るために、いくつかのマイナーな改良と、それはより安全に行うことができます:

public static void main(String[] args) { 
    Comparable<?> o1 = getComparable(Math.random()); 
    Comparable<?> o2 = getComparable(Math.random()); 
    if (o1.getClass().equals(o2.getClass())) { 
     // safe cast 
     Comparable c1 = (Comparable) o1; 
     Comparable c2 = (Comparable) o2; 
     // safe comparison 
     int x = c1.compareTo(c2); 
     System.out.println(x); 
    } 
} 

public Comparable<? extends Comparable<?>> getComparable(double d) { 
    if (d < 0.5) return (new Car()); 
    return (new Apple()); 
} 
+0

ありがとうございます。実際には、getObject()メソッドはライブラリによって与えられているので、私はそれを制御することができません。さもなければ私はあなたの提案に従います。私はちょうど警告を抑制する必要はありません。より良い方法があるはずです(しかし明らかにそうではありません)。 – Remirror

+0

"は、2つのオブジェクトが同じクラスであることを保証するので、警告は無視しても安全です。クラスは、型安全性の観点からクラス自体に匹敵するとは保証されません。 – newacct

-1

Comparableオブジェクトではなく、Objectオブジェクトを扱うためにあなたの方法を更新します。

public class MyClass { 

    public static void main(String[] args) {  
    Comparable o1 = getObject(Math.random()); 
    Comparable o2 = getObject(Math.random()); 
    if (o1.getClass().equals(o2.getClass()) { // two Cars or two Apples 
     int x = o1.compareTo(o2); 
     System.out.println(x); 
    ) 
    } 

    public Comparable getObject(double d) { // given method that may not be changed 
    if (d < 0.5) return (new Car()); // Car implements Comparable<Car> 
    else return (new Apple()); // Apple implements Comparable<Apple> 
    } 

} 
0

ここでの問題は、Comparable<T>がちょうどT Sを比較する「快適」な特徴を持っているようComparable<Car>Comparable<Apple>は、互いに比較することができないということです。そして今、私たちはそれを支払っています。

.equalsの場合、これは該当しません。私たちは、これは非常に最小限の表現型システムで、ジャワの特徴である

// WILL NOT WORK 

if (o1.getClass().equals(o2.getClass()) { // two Cars or two Apples 
    int x = comparison(o1.getClass(), o1, o2); 
    System.out.println(x); 
) 

static <T extends Comparable<T>> int comparison(Class<T> type, T o1, T o2) { 
    return type.cast(o1).compareTo(type.cast(o2)); 
} 

をしようとしている何

-1

別の代替実装:Aominè@

public static void main(String[] args) {  
    Object o1 = getObject(Math.random()); 
    Object o2 = getObject(Math.random()); 
    if (o1.getClass().equals(o2.getClass()) && o1 instanceof Comparable) { 
     int x = getObject(o1).compareTo(o2); 
     System.out.println(x); 
    } 

    } 

    public static Object getObject(double d) { // given method that may not be changed 
    if (d < 0.5) return (new Car()); // Car implements Comparable<Car> 
    else return (new Apple()); // Apple implements Comparable<Apple> 
    } 

    public static <T> Comparable<T> getObject(Object o) { 
     return (Comparable<T>)o; 
    } 
関連する問題