2012-07-16 69 views
2

我的代碼是: -字節類不泛型工作在java中

class abc<T> { 
    T a, b; 
    abc(T p, T q) { 
        a = p; 
        b = q; 
    } 
    void disp() { 
        System.out.println("\na = " + a); 
        System.out.println("b = " + b); 
        System.out.println("a/b is of class type : " + a.getClass().getName()); 
    } 
} 

class temp { 
    public static void main(String...args) { 
        abc<Integer> a1; 
        a1 = new abc <Integer>(11, 22); 
        abc<Byte> a2 = new abc <Byte>(50,5); 
        a1.disp(); 
        a2.disp(); 
    } 
} 

OUTPUT: -

temp.java:23: cannot find symbol 
symbol : constructor abc(int,int) 
location: class abc<java.lang.Byte> 
      abc <Byte> a2 = new abc <Byte> (50,5); 
          ^
1 error 

請幫我出這個問題。我是java新手,所以要學習泛型。

在這段代碼文中,我用整數,浮點數,雙精度,字符串都工作正常,但當我到達字節類編譯器會引發錯誤。

+0

歡迎來到Java。類名的Java約定是以大寫字母開頭,所以'Abc'。這使得他人更容易閱讀你的代碼 – 2012-07-16 19:33:52

回答

6

這個怎麼樣?

abc <Byte> a2 = new abc <Byte> ((byte)50, (byte)5); 

您提供的數字文字的參數是整型的,和那些被自動裝箱爲java.lang.Integer的,這就是爲什麼一個相應的方法最初沒有找到,除非你明確地說你的文字是類型字節。

+0

ohhh ...多數民衆贊成在問題蔓延....... thanx人.... :-) – 2012-07-16 19:30:03