【问题标题】:How to initialize a tuple class in implementation in java如何在java中的实现中初始化一个元组类
【发布时间】:2014-10-05 14:20:44
【问题描述】:
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 可能未初始化”的错误。我不明白如何从制作像 Base 和 Power 这样的泛型并将它们用作整数,而我尝试的一切似乎都是错误的。我真的可以使用一些帮助,但我认为我对术语的理解不足以进行搜索。

感谢您的帮助。

编辑:非常感谢!

【问题讨论】:

  • Term&lt;Integer, Integer&gt; t; 未初始化。

标签: java initialization tuples implementation


【解决方案1】:

t 未初始化,因为您从未为其分配任何内容。这是解决此问题的一种方法:

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

【讨论】:

    【解决方案2】:

    您的 t 变量没有值。

    您需要将您的类的new 实例放入其中,就像任何其他类一样。

    【讨论】:

      猜你喜欢
      • 2010-12-28
      • 2011-03-10
      • 1970-01-01
      • 2011-08-18
      • 1970-01-01
      • 2012-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多