2011-11-20 57 views
1

我試圖實現add方法中提到的符合found java.lang.Integer Required Tin the Generic sparse matrix addition questionJava的泛型此外

class Matrix<T extends Number> 
{ 
    private T add(T left, T right) 
    { 
    if (left instanceof Integer) 
    { 
    return new Integer(((Integer)left).intValue() + ((Integer)right).intValue()); 
    } 
} 

的編譯器錯誤,我返回一個新的整數。我不確定自從T擴展Number並且Integer是Number的子類後我缺少的。

+0

'Cat'擴展'Animal','Dog'是'Animal'的一個子類。這並不意味着你可以返回一個'狗',其中預計有'貓'。 –

+0

@OliCharlesworth我已經嘗試過鑄造到T,但是這似乎沒有幫助 –

+0

@NuclearGhost:這給了什麼錯誤? – SLaks

回答

4

編譯器不會讓你這樣做,因爲T可能是其他一些類,如Double
你知道TInteger檢查instanceof檢查,但編譯器沒有。

0

「我不知道自從T擴展Number並且Integer是Number的一個子類後我缺少的東西。」

該聲明是錯誤的。一般來說,如果您有:

public class B extends A { 
} 

public class C extends A { 
} 

這並不意味着B可以被轉換爲C。所以寫的東西,如:

public <T extends A> T method(T arg) { 
    return (B)arg; 
} 

和你B b = (B)method(C);調用它顯然是錯誤的。

+0

啊,是的,這是正確的。僅僅因爲它們延伸相同的東西並不能使它們相同。 –

2

Java的類型系統根本無法表達這一點。這是一個解決方法。

創建提供了數字運算你有興趣的接口Numeric,並寫入它的實現你感興趣的數據類型。

interface Numeric<N> { 
    public N add(N n1, N n2); 
    public N subtract(N n1, N n2); 
    // etc. 
} 

class IntNumeric extends Numeric<Integer> { 
    public static final Numeric<Integer> INSTANCE = new IntNumeric(); 

    private IntNumeric() { 
    } 

    public Integer add(Integer a, Integer b) { 
    return a + b; 
    } 

    public Integer subtract(Integer a, Integer b) { 
    return a - b; 
    } 

    // etc. 
} 

和重寫你的Matrix類的構造函數接受這個執行。

class Matrix<N> { 
    private final Numeric<N> num; 
    private final List<List<N>> contents; 

    public Matrix(Numeric<N> num) { 
    this.num = num; 
    this.contents = /* Initialization code */; 
    } 

    public Matrix<N> add(Matrix<N> that) { 
    Matrix<N> out = new Matrix<N>(num); 
    for(...) { 
     for(...) { 
     out.contents.get(i).set(j, 
      num.add(
      this.contents.get(i).get(j), 
      that.contents.get(i).get(j), 
     ) 
     ); 
     } 
    } 
    return out; 
    } 
} 

// Use site 
Matrix<Integer> m = new Matrix<Integer>(IntNumeric.INSTANCE); 

希望有所幫助。

-2

包裝泛型;

public class Box<T> { 

     public T j,k; 
     int l; 
     float f; 

     @SuppressWarnings("unchecked") 
    public void add(T j,T k) { 
     this.j = j; 
     this.k=k; 

     if(j.toString().contains(".")) 
     { 
       this.f=Float.parseFloat(j.toString())+Float.parseFloat(k.toString()); 


     } else{ 
     this.l=Integer.parseInt(j.toString())+Integer.parseInt(k.toString()); 
     } 
     } 

     public int getInt() { 
     return l; 
     } 

     public float getFloat() { 
      return f; 
      } 

     public static void main(String[] args) { 
     Box<Integer> integerBox = new Box<Integer>(); 
     Box<Float> floatBox = new Box<Float>(); 

     integerBox.add(new Integer(10),new Integer(20)); 
     floatBox.add(new Float(2.2),new Float(3.3)); 

     System.out.printf("Integer Value :%d\n\n", integerBox.getInt()); 
     System.out.printf("float Value :%f\n", floatBox.getFloat()); 
     } 
    } 
+0

你不應該在沒有任何解釋的情況下發布代碼,它可以或應該如何解決問題。順便說一句,你也不應該在代碼中添加@SuppressWarnings(「unchecked」)',因爲你故意這樣做。另一件事:你的代碼對於異常非常脆弱。 – Tom