简体   繁体   English

如何在不同的类中调用void方法?

[英]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));    
    }
}

I have to print solution1 and soltion2 at different class(tester) by calling them.我必须通过调用它们在不同的类(测试人员)上打印solution1soltion2

It is a void method call, so it doesn't returning anything.这是一个void方法调用,因此它不返回任何内容。

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 :)谢谢你们帮助我:)

This is how you call a static void method in another class.这就是您在另一个类中调用static void方法的方式。

== file exercise/Equation.java == == 文件练习/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 == == 文件 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:关键是:

  1. If you want to use Equation in another class, it is best if you declare the class in a Java package.如果要在另一个类中使用Equation ,最好在Java 包中声明该类。

  2. Then you needs to import the Equation into the other class.然后,你需要importEquation为其他类。 (This is not necessary if both classes are in the same package.) (如果两个类都在同一个包中,这不是必需的。)

  3. Obviously a void method cannot return anything.显然, void方法不能返回任何内容。 So if you want some output you from your quadEquation method, then the method needs to print it itself.因此,如果您想从quadEquation方法中获得一些输出,则该方法需要自行打印它。

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.例如,您可能希望quadEquation方法可用于在其他地方输出一个或多个解决方案的上下文中……或根本不输出。

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 .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.很可能你的老师已经为你解决了调用quadEquation的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM