2014-10-05 167 views
2
class Term <Base, Power>{ //declaration of new class Term 
    Base a; 
    Power b; 
    public Term(Base a, Power b) { 
     this.a = a; 
     this.b = b; 
    } 
} 


public static void in(){ //trying to use this class in another class's method 
    Scanner input = new Scanner(System.in); 
    System.out.println("Enter two integers: "); 
    Term<Integer, Integer> t; 
    t.a = input.nextInt(); 
    t.b = input.nextInt(); 


} 

我得到一個「t可能未初始化」的錯誤。我不明白如何從製作基本和力量這樣的泛型並將它們用作整數,而我嘗試的所有東西似乎都是錯誤的。我真的可以使用一些幫助,但我不認爲我理解的術語足以進行搜索。如何在java中實現元組類的初始化

感謝您的任何幫助。

編輯:非常感謝!

+0

'Term t;'未初始化。 – Maroun 2014-10-05 14:22:28

回答

2

t未初始化,因爲您從不向其分配任何內容。這裏有一種方法可以解決這個問題:

int a = input.nextInt(); 
int b = input.nextInt(); 
Term<Integer, Integer> t = new Term<Integer, Integer>(a, b); 
0

您的t變量沒有任何價值。

您需要將類的new實例放入其中,就像任何其他類一樣。