很長一段時間,我一直在試圖讓一段代碼工作,而這是不可能的。我有類需要一個通用的,我已經設置爲整數。所以我嘗試了follwoing:嚴重的泛型問題:整數與整數java
public class a<T> //set generics for this function
{
private T A;
protected boolean d;
public a(final T A)
{
this.A = A;
//do calculations
//since constructor cannot return a value, pass to another function.
this.d = retnData(Integer.parseInt(A.toString()) != 100); //convert to an integer, the backwards way.
}
private boolean retnData(boolean test)
{
return test;
}
}
// IN ANOTHER CLASS
transient a<Integer> b;
boolean c = b.d = b.a(25); // throws an erorr: (Int) is not apporpriate for a<Integer>.a
Java將不允許這種由於Java認爲是INT =整型,即使雙方接受相同的數據!並且由於泛型工作的方式,我不能設置b;因爲int類型的原始性。即使轉換爲整數也不起作用,因爲它仍然會拋出「類型錯誤」,我必須問是否存在某種解決方法,或者試圖使其工作無效?
如果您不想混淆代碼,每個想幫助您的人都需要去混淆,這將會有很大的幫助。 –