2011-08-17 132 views
1

我收到運行時錯誤Exception in thread "main" java.lang.ClassCastException: m.compiler.datatypes.Int cannot be cast to java.lang.IntegerClassCastException異常

以下是代碼片段:

public abstract class DataType<E> { 
protected E value; 
... 
} 

而且這裏的類int:

public class Int extends DataType<Integer> { 

@Override 
public DataType<Integer> compare(DataType<Integer> other) { 
    if(value > other.value) //here the exception is thrown 
     return new Int(1); 
    if (value < other.value) 
     return new Int(-1); 
    return new Int(0); 
} 

}

實際通話:

 if(lValue instanceof Int) { 
      Int lInt = (Int) lValue; 
      Int result = (Int) lInt.compare(rInt); 

如果我打印裏面的值進行比較的值是正確的,但即使我

other.value.getClass() 

檢查我得到的例外。編譯器沒有錯誤,也沒有警告。

+0

你是怎麼稱呼它的? – Bohemian 2011-08-17 10:34:23

回答

1

在某些情況下,您必須分配Intvalue ...並且應該有關於此的警告。你能製作valuefinal