简体   繁体   English

从单独的 class 运行

[英]run from a separate class

So i need to run this class from a second class.所以我需要从第二个 class 运行这个 class。 How do I do that?我怎么做? Here's the code I want to run from a separate class:这是我想从单独的 class 运行的代码:

import java.util.*;

public class Numbers {

    int min(int a, int b, int c, int d, int e) {
        int min1 = Math.min(a, b);
        int min2 = Math.min(c, d);
        int min3 = Math.min(min1, min2);
        int min4 = Math.min(min3, e);

        return min4;
    }
    int max(int a, int b, int c, int d, int e) {
        int max1 = Math.max(a, b);
        int max2 = Math.max(c, d);
        int max3 = Math.max(max1, max2);
        int max4 = Math.max(max3, e);

        return max4;
    }
    double avg(int a, int b, int c, int d, int e) {
        double average = (a+b+c+d+e)/5;

        return average;
    }
    double stddev(int a, int b, int c, int d, int e) {
        double average = (a+b+c+d+e)/5;
        double a1 = Math.pow((a-average),2);
        double b1 = Math.pow((b-average),2);
        double c1 = Math.pow((c-average),2);
        double d1 = Math.pow((d-average),2);
        double e1 = Math.pow((e-average),2);
        double average2 = (a1+b1+c1+d1+e1)/5;
        double x;
        x = Math.sqrt(average2);
        return x;

    }

}

Somewhere in another class, you instantiate this class and call appropriate methods:在另一个 class 的某处,您实例化此 class 并调用适当的方法:

Numbers instance = new Numbers();
int maxValue = instance.max(1, 2, 3, 4, 5);

You mean call the methods in the Numbers class?您的意思是调用数字 class 中的方法? You can do this as follows:您可以按如下方式执行此操作:

public Main{
    public static void main( String[] args ){
            Numbers numbers = new Numbers(); // Instance

            int minimum = numbers.min( 1, 2, 3, 4, 5 );
            System.out.println( "minimum:" + minimum );

            int maximum = numbers.max( 1, 2, 3, 4, 5);
            System.out.println("maximum:" + maximum );

            double average = numbers.avg( 1, 2, 3, 4, 5 );
            System.out.println("average:" + average );

            double standardDev = numbers.devstd( 1, 2, 3, 4, 5 );
            System.out.println("standard deviation:" + standardDev );
    }   
}

Better yet, make those methods static since they don't depend on any attributes:更好的是,使这些方法 static 因为它们不依赖于任何属性:

public static int min(int a, int b, int c, int d, int e) {
    int min1 = Math.min(a, b);
    int min2 = Math.min(c, d);
    int min3 = Math.min(min1, min2);
    int min4 = Math.min(min3, e);

    return min4;
}

public Main{
    public static void main( String args[] ){
            int minimum = Numbers.min( 1, 2, 3, 4, 5);
            System.out.println( "minimum:" + minimum );
    }   
}

Added bonus with a more general-purpose method:使用更通用的方法增加了奖励:

public static int min(int[] intArray ) {
    // If empty/null array throws Exception. Caller/supplier should handle this.
    int min = intArray[0]; 

    for( int i = 1; i < intArray.length; i++ ){
            min = Math.min(min, intArray[i]);
    }

    return min;
}

Create an object of this class in the other class ( Note: in the same package).在另一个 class 中创建此 class 的 object(注意:在同一包中)。

Like this,像这样,

Numbers n = new Numbers();
int minimum = n.min(10,20,30,40,50);
System.out.println(minimum);          // Prints 10
int maximum = n.max(10,20,30,40,50);
System.out.println(maximum);          // Prints 50
double average = n.avg(10,20,30,40,50);
System.out.println(average);          // Prints 30
double dev = n.devstd(10,20,30,40,50);

If your class is in the same package (if you are using the default setup in Eclipse for example, then it will be), then in your new class you can call these functions just like you did with Math.pow If your class is in the same package (if you are using the default setup in Eclipse for example, then it will be), then in your new class you can call these functions just like you did with Math.pow

double l_stddev = Numbers.stddev(1,2,3,4,5);

for example.例如。

Edit: Oops - missed that these were not static methods.编辑:哎呀-错过了这些不是 static 方法。 You could make them static in your Numbers class, then you would not need to make an object to call them (just like the Math class in fact)您可以在您的数字 class 中将它们设为 static,然后您就不需要创建 object 来调用它们(就像 Math6BBBDZABC 实际上一样)

So as the other answer says, with you class as it is, you need to make a Number object.因此,正如另一个答案所说,对于 class,您需要输入一个数字 object。

In the another class, in main() method create an object of Numbers class and call the desired methods.在另一个 class 中,在 main() 方法中创建数字 class 的 object 并调用所需的方法。

    Numbers obj=new Numbers();
     obj.max( a,  b,  c,d ,e);

I don't know what you mean by run from a separate class, but if you want to call these functions from another class you should make them static:我不知道你从一个单独的 class 运行是什么意思,但是如果你想从另一个 class 调用这些函数,你应该将它们设为 static:

public static int min(...

Then you call it:然后你叫它:

int m = Numbers.min(...

from wherever you want.从你想要的任何地方。

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

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