英文:
How to call void method in different class?
问题
public class Equation {
public static void quadEquation(double a, double b, double c) {
double solution1 = (-b + (Math.pow(b,2)-(4*a*c))/(2 * a));
double solution2 = (-b - (Math.pow(b,2)-(4*a*c))/(2 * a));
}
}
我需要在不同的类(tester)中通过调用它们来打印solution1和solution2。
这是一个void
方法调用,所以它不会返回任何值。
在这种情况下,我该如何在tester中调用它们呢?
家庭作业任务!这只是我的家庭作业的一部分,所以你们可以自由地帮助我
我已经解决了!感谢帮助过我的人们
<details>
<summary>英文:</summary>
public class Equation {
public static void quadEquation(double a, double b, double c) {
double solution1 = (-b + (Math.pow(b,2)-(4*a*c))/(2 * a));
double solution2 = (-b - (Math.pow(b,2)-(4*a*c))/(2 * a));
}
}
I have to print **solution1** and **soltion2** at different class(tester) by calling them.
It is a `void` method call, so it doesn't returning anything.
In this case how can I call them in tester?
Homework assignment! It is just part of my homework so you guys can freely help this one
I solved it! Thanks for those of you helped me :)
</details>
# 答案1
**得分**: 2
这是如何调用另一个类中的 `static void` 方法的示例。
== 文件 exercise/Equation.java ==
```java
package exercise;
public class Equation {
public static void quadEquation(double a, double b, double c) {
double solution1 = (-b + (Math.pow(b, 2) - (4 * a * c)) / (2 * a));
double solution2 = (-b - (Math.pow(b, 2) - (4 * a * c)) / (2 * a));
// 如果你想要打印解决方案,请在这里添加一些打印语句!
}
}
== 文件 OtherClass.java ==
import exercise.Equation;
public class OtherClass {
public static void main(String[] args) {
Equation.quadEquation(1.0, 2.0, 3.0); // 调用 Equation 中的方法
}
}
你应该能够按照以下方式从命令行编译和运行上述代码。
$ # 切换到包含 "OtherClass.java" 的目录
$ javac -cp . exercise/Equation.java OtherClass.java
$ java -cp . OtherClass
关键点是:
-
如果你想在另一个类中使用
Equation
,最好将该类声明为 Java 包中的一部分。 -
然后,你需要将
Equation
导入到其他类中。(如果两个类在同一个包中,则不需要此操作。) -
显然,
void
方法无法返回任何内容。因此,如果你希望从quadEquation
方法中获得一些输出,那么方法需要自己进行打印。
请注意,在前面提到的第三点中描述的方法通常不是一个良好的设计选择。通常情况下,你不希望混合代码的 "concerns"(关注点)像那样。通常情况下,你希望将计算和输出结果视为独立的问题。例如,你可能希望在其他地方输出解决方案,或者根本不输出解决方案。
其他注意事项:
-
如果这是用于找到二次方程的根,那么你没有正确编码公式。公式中应该有一个 "平方根",请参阅 https://en.wikipedia.org/wiki/Quadratic_formula。
-
你还需要考虑解是虚数的情况。(除非你被告知忽略这一点...)
-
我看到你已经注释说其他类是一个驱动程序。这可能使上述示例无效。你将不得不阅读驱动程序类的代码(或类的描述)来弄清楚它将如何调用你的类。很可能问题已经由你的教师为你解决了。
英文:
This is how you call a static void
method in another class.
== file exercise/Equation.java ==
package exercise;
public class Equation {
public static void quadEquation(double a, double b, double c) {
double solution1 = (-b + (Math.pow(b,2)-(4*a*c))/(2 * a));
double solution2 = (-b - (Math.pow(b,2)-(4*a*c))/(2 * a));
// If you want to print the solutions, add some print
// statements here!
}
}
== file OtherClass.java ==
import exercise.Equation;
public class OtherClass {
public static void main(String[] args) {
Equation.quadEquation(1.0, 2.0, 3.0); // calls the method in Equation
}
}
You should be able to compile and run the above from the command line as follows.
$ # change directory to directory containing "OtherClass.java"
$ javac -cp . exercise/Equation.java OtherClass.java
$ java -cp . OtherClass
The key things are:
-
If you want to use
Equation
in another class, it is best if you declare the class in a Java package. -
Then you needs to
import
theEquation
into the other class. (This is not necessary if both classes are in the same package.) -
Obviously a
void
method cannot return anything. So if you want some output you from yourquadEquation
method, then the method needs to print it itself.
Note that the approach described in the previous point is typically a pour design choice. You typically don't want to mix the "concerns" of the code like that. You typically want to treat the calculation and outputting the results as separate problems. For example, you may want your quadEquation
method to be usable in contexts where you output the solution or solutions somewhere else ... or not at all.
Other things:
-
If this is supposed to find the roots of a quadratic equation, you have not coded the formula correctly. There should be a "square root" in there; see https://en.wikipedia.org/wiki/Quadratic_formula.
-
You also need to consider the case where the solutions are imaginary numbers. (Unless you have been told to ignore that ...)
-
I see that you have commented to say that the other class is a driver. This may make the above example invalid. You are going to have to read the code of the driver class (or the description of the class) top work out how it is going to call your class. It may well be that the problem of calling your
quadEquation
has been solved for you by your teacher.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论