【问题标题】:How can I call a class in another class [closed]我怎样才能在另一个类中调用一个类[关闭]
【发布时间】:2014-10-31 14:45:36
【问题描述】:

假设我有一个 Main 类,我的主程序用它运行。

public calss Main{
   public static void main(String[] args){
       System.out.print("input Length ");
       a = in.nextInt();
       System.out.print("input Height ");
       b = in.nextInt();
       ...
       (The code that goes in between?)
       ...
       System.out.println("output");
   }
}

如何使用另一个类并在我的第一个类中输入它让我们说它是否像一个简单的计算类

pubic class Math{
    output = a*b
}

并且有这样的输入和输出:

input Length 2
input Height 3
6

顺便说一句,不要投票给我,因为我是菜鸟!你为什么这样做?呵呵

【问题讨论】:

标签: java class input output


【解决方案1】:

就这么简单。

public class Test{
  public int multiplication(int a, int b){
   return a*b;
  }

  public static void main(String[] args){
       System.out.print("input Length ");
       a = in.nextInt();
       System.out.print("input Height ");
       b = in.nextInt();
       ...
       Test t = new Test();
       System.out.println(t.multiplication(a,b));
   }
}

【讨论】:

  • 此答案假设您希望使用 Test 类的实例 - 没有必要这样做,因为乘法是无状态的。在此示例中,由于它与 main 方法位于同一类中,因此我建议将其作为静态函数调用。
  • 对解决方案的解释会很棒
  • 另外,代码无法编译。
【解决方案2】:

你把一个类和一个方法搞混了。

如果你想把你的计算方法放在一个类中

例如

public class MyCalc {
    public static int calculate(int a, int b) { 
         return a*b;
    }
}

然后你可以用你的 main 调用那个函数

public static void main(String[] args) {

     // something


     int result = MyCalc.calculate(1,2);
} 

这就是您在实用程序类中使用静态函数来模块化某些功能的方式。这有帮助吗?

【讨论】:

    【解决方案3】:

    你的第二个类也可能有字段和方法。对于您的示例,您的 Math 类应该有一个方法,如果您执行两个整数的乘法,它应该接收这些整数作为参数。这是一个小例子:

    public class Math {
        //declaring the method as static
        //no need to create an instance of the class to use it
        //the method receives two integer arguments, a and b
        //the method returns the multiplication of these numbers
        public static int multiply(int a, int b) {
            return a * b;
        }
    }
    

    但是要小心,不要用Java内置类的名字来命名你的类,**特别是java.lang包中的类。是的,Java 中有一个内置的Math 类。

    因此,最好将您的课程重命名为:

    public class IntegerOperations {
        public static int multiply(int a, int b) {
            return a * b;
        }
    }
    

    你会像这样使用它(在修复你当前的代码之后):

    public class Main {
        public static void main(String[] args) {
            //Use a Scanner to read user input
            Scanner in = new Scanner(System.in);
    
            System.out.print("input Length ");
            //declare the variables properly
            int a = in.nextInt();
            System.out.print("input Height ");
            int b = in.nextInt();
    
            //declare another variable to store the result
            //returned from the method called
            int output = Operations.multiply(a, b);
    
            System.out.println("output: " + output);
        }
    }
    

    【讨论】:

    • 哦,对了,我忘了数学课了,谢谢你,虽然很有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 2020-07-24
    相关资源
    最近更新 更多