lets say I have a Main class which my main program runs with it.
假设我有一个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");
}
}
How can I use another class and input it in side my first class lets say if its a simple calculation class like
我如何使用另一个类并在第一个类的侧面输入它,让我们说它是一个简单的计算类
pubic class Math{
output = a*b
}
and have like this input and output:
并喜欢这个输入和输出:
input Length 2
input Height 3
6
By the way don't vote down for me cause I'm noob! common why you do this? XD
顺便说一句,不要为我投票,因为我是菜鸟!你为什么这么做? XD
3 个解决方案
#1
2
Its simple as that.
它很简单。
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));
}
}
#2
1
You are confusing a class with a method there.
你在课堂上混淆了一个方法。
If you want to put your calculation method in a class
如果要将计算方法放在类中
e.g.
public class MyCalc {
public static int calculate(int a, int b) {
return a*b;
}
}
Then you could call that function from with your main
然后你可以用你的主要来调用那个功能
public static void main(String[] args) {
// something
int result = MyCalc.calculate(1,2);
}
That's how you'd use static functions in a utility class to modularise some functionality. Does that help?
这就是你如何在实用程序类中使用静态函数来模块化某些功能。这有帮助吗?
#3
1
Your second class may have fields and methods as well. For your example, your Math
class should have a method were you perform the multiplication of two integers, and it should receive these integers as parameters. Here's a small example on it:
你的第二堂课也可能有田野和方法。对于您的示例,您的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;
}
}
But be careful, do not name your class with the same name of built-in classes in Java, **specially classes in java.lang
package. Yes, there is a built-in Math
class in Java.
但是要小心,不要在Java中使用相同的内置类命名您的类,**特别是java.lang包中的类。是的,Java中有一个内置的Math类。
So, it would be better to rename your class to something like this:
因此,最好将您的类重命名为以下内容:
public class IntegerOperations {
public static int multiply(int a, int b) {
return a * b;
}
}
And you will use it like this (after fixing your current code):
你会像这样使用它(修复你当前的代码后):
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);
}
}
#1
2
Its simple as that.
它很简单。
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));
}
}
#2
1
You are confusing a class with a method there.
你在课堂上混淆了一个方法。
If you want to put your calculation method in a class
如果要将计算方法放在类中
e.g.
public class MyCalc {
public static int calculate(int a, int b) {
return a*b;
}
}
Then you could call that function from with your main
然后你可以用你的主要来调用那个功能
public static void main(String[] args) {
// something
int result = MyCalc.calculate(1,2);
}
That's how you'd use static functions in a utility class to modularise some functionality. Does that help?
这就是你如何在实用程序类中使用静态函数来模块化某些功能。这有帮助吗?
#3
1
Your second class may have fields and methods as well. For your example, your Math
class should have a method were you perform the multiplication of two integers, and it should receive these integers as parameters. Here's a small example on it:
你的第二堂课也可能有田野和方法。对于您的示例,您的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;
}
}
But be careful, do not name your class with the same name of built-in classes in Java, **specially classes in java.lang
package. Yes, there is a built-in Math
class in Java.
但是要小心,不要在Java中使用相同的内置类命名您的类,**特别是java.lang包中的类。是的,Java中有一个内置的Math类。
So, it would be better to rename your class to something like this:
因此,最好将您的类重命名为以下内容:
public class IntegerOperations {
public static int multiply(int a, int b) {
return a * b;
}
}
And you will use it like this (after fixing your current code):
你会像这样使用它(修复你当前的代码后):
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);
}
}