从同一个 java 项目中的另一个类调用枚举值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12947125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:54:22  来源:igfitidea点击:

Calling a Enum value from another class within the same java project

javaclassenums

提问by Doug Swanson

I have an assignment in my introduction to Java class where we are suppose to read from a file, conduct mathematical operations on it, then output it to another file. i seem to have the file I/O portion down. The problem that I am having is in the mathematical operation stage. We were given a class, called SimpleMath, and are suppose to call methods from it. here is the given class:

我在 Java 类的介绍中有一个作业,我们假设从文件中读取数据,对其进行数学运算,然后将其输出到另一个文件。我似乎把文件 I/O 部分放下了。我遇到的问题是在数学运算阶段。我们得到了一个名为 SimpleMath 的类,并假设从中调用方法。这是给定的类:

import java.text.DecimalFormat;

public class SimpleMath {

    // Operation enumeration
    public enum eOperation {
        PERIMETER, AREA
    }

    // Conversion enumeration
    public enum eScale {
        CELSIUSFAHRENHEIT, FAHRENHEITCELSIUS
    }    


    // computeHypotenuse method
    public static double computeHypotenuse(double adjacent, double opposite) {

        // Pythagorean theorem
        return Math.sqrt(adjacent * adjacent + opposite * opposite);
    }

    // solveQuadratic method
    public static double solveQuadratic(double a, double b, double c) {

        // Quadratic formula
        double minusRoot = (-b - Math.sqrt((b * b) - (4.0 * a * c))) / (2.0 * a);
        double plusRoot  = (-b + Math.sqrt((b * b) - (4.0 * a * c))) / (2.0 * a);
        return (plusRoot >= minusRoot ? plusRoot : minusRoot);
    }

    // convertTemperature method
    public static double convertTemperature(eScale scale, double degrees) {

        // Scale conversion
        if (scale == eScale.CELSIUSFAHRENHEIT)
            return (((9.0 / 5.0) * degrees) + 32.0);
        else
            return ((5.0 / 9.0) * (degrees - 32.0));
    }

    // geometryCircle method
    public static double geometryCircle(eOperation op, double radius) {

        // Basic geometry
        if (op == eOperation.PERIMETER)
            return (Math.PI * radius * 2.0);
        else
            return (Math.PI * radius * radius);
    }
}

The Problem that I have been having is in my code when i try to call the methods geometryCircle and convertTemperature from the SimpleMath class. This is because the two methods use enum variables in the calling of the variables entering the class. It turns out that no matter what I seem to do the P5 class that I am working in is having problems passing these enum variables between classes. Here is the P5 code i have written so far:

当我尝试从 SimpleMath 类调用方法 geometryCircle 和 convertTemperature 时,我遇到的问题出在我的代码中。这是因为这两个方法在调用进入类的变量时都使用了枚举变量。事实证明,无论我似乎在做什么,我正在处理的 P5 类都在在类之间传递这些枚举变量时遇到问题。这是我到目前为止编写的 P5 代码:

  import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Scanner;

    import SimpleMath.eOperation;
    import SimpleMath.eScale;


public class P5 
{   

// These are the input variables used
    double Adjacent,Opposite,CoEff0, CoEff1, CoEff2, Fahrenheit,Radius;

    // These are the variables used for output values
    double Hypotenuse, Root, Celsius, Area;

    // This is the main method of class P5 main method that tells the other methods in this class where to find their variables.    
    public static void main(String[] args) 
    {
        // Instantiates the Class P5 into variable p5.
        P5 p5=new P5();

        // Initiates the readFile method while passing it the arguments that are stored in the class.
        p5.readFile(args[0]);

        // Initiates the computeMath method with no added variables.
        p5.computeMath();

        // Initiates the writeFile method and passes the desired name, which is stored as a part of the class, of the new file being written to. 
        p5.writeFile(args[1]);

    }


    /* This method tells the computer to read in a file,
     * which is specified in the RunConfigurations window under the Arguments tab in the file for this class,
     * and then it assigns the values pulled from the file to the appropriate input variables
     */
    private void readFile(String InputFile)
    {
        try
            {
                // This tells the computer how and where to look for the designated input file.
                Scanner ReadFile = new Scanner(new File(InputFile));

                // These set up variables so that all of the lines in the files can be assigned later.
                String Line1 = ReadFile.nextLine();
                String Line2 = ReadFile.nextLine();
                String Line3 = ReadFile.nextLine();
                String Line4 = ReadFile.nextLine();
                String Line5 = ReadFile.nextLine();
                String Line6 = ReadFile.nextLine();
                String Line7 = ReadFile.nextLine();

                // These are the variables, and here they are being assigned values from the file that is being read.
                Adjacent = Double.valueOf(Line1);
                Opposite = Double.valueOf(Line2);
                CoEff0 = Double.valueOf(Line3);
                CoEff1 = Double.valueOf(Line4);
                CoEff2 = Double.valueOf(Line5);
                Fahrenheit = Double.valueOf(Line6);
                Radius = Double.valueOf(Line7);

                // This lets the user see what values have been written to the variables, mostly for error checking purposes.
                System.out.println("Adjacent: " + Adjacent);
                System.out.println("Opposite: " + Opposite);
                System.out.println("CoEff0: " + CoEff0);
                System.out.println("CoEff1: " + CoEff1);
                System.out.println("CoEff2: " + CoEff2);
                System.out.println("Fahrenheit: " + Fahrenheit);
                System.out.println("Radius: " + Radius);

                // This closes the designated file and disconnects it from the stream.
                ReadFile.close();
            } 
        catch (IOException e)

            {
                System.out.println("Cannot Read File: " + InputFile);
                System.exit(0);
            }
    }

    // This method takes the values found in the ReadFile method and conducts mathematical operations on them.
    private void computeMath()
    {
        // This calls the 
        Hypotenuse = SimpleMath.computeHypotenuse(Adjacent,Opposite);
        Root = SimpleMath.solveQuadratic(CoEff0, CoEff1, CoEff2);       
        Celsius = SimpleMath.convertTemperature(eScale.FAHRENHEITCELSIUS, Fahrenheit);
        Area = SimpleMath.geometryCircle(eOperation.AREA, Radius);
    }


    /* This method takes all of the variables values determined throughout the program
     * and writes them to a file that is specified in the RunConfigurations window under
     * the Arguments tab for this class. 
     */
    private void writeFile(String OutputFile)
    {
        try
            {
                // This initiates the PrintWriter class which allows the program to write to a file.
                PrintWriter Output = new PrintWriter(new File(OutputFile));

                // These are the lines of text in which the program is writing in the designated file. 
                Output.println("Adjacent = " + Adjacent);
                Output.println("Opposite = " + Opposite);
                Output.println("Coefficent 0 = " + CoEff0);
                Output.println("Coefficent 1 = " + CoEff1);
                Output.println("Coefficent 2 = " + CoEff2);
                Output.println("Fahrenheit = " + Fahrenheit);
                Output.println("Radius Of Circle = " + Radius);

                // This closes the designated file and disconnects it from the stream.
                Output.close();

            }   
        catch(IOException e)



            {
                    System.out.println("Cannot Write File: " + OutputFile);
                    System.exit(0);
                }
        }

}

So any help would be nice, even speculations into why this code is behaving the way that it is. Thanks

因此,任何帮助都会很好,甚至可以推测为什么此代码的行为方式如此。谢谢

回答by Yogendra Singh

Simply add the class name in from of the enumsas below. As they re declared public, you should be fine.

只需在enums下面添加类名。当他们重新公开时,你应该没事。

Celsius = SimpleMath.convertTemperature(
                                  SimpleMath.eScale.FAHRENHEITCELSIUS, Fahrenheit);
Area = SimpleMath.geometryCircle(SimpleMath.eOperation.AREA, Radius);

回答by Priya Kadam

To use enum values declared in one package into some other package,

要将一个包中声明的枚举值用于其他包,

  1. make sure you declare the enum as public.

    public enum DATATYPE { STRING, LONG, BOOLEAN, INTEGER; }

  2. Import the enum in the package where you want to access the values.

    import Helper.DATATYPE;

  1. 确保将枚举声明为公共。

    public enum DATATYPE { STRING, LONG, BOOLEAN, INTEGER; }

  2. 在要访问值的包中导入枚举。

    import Helper.DATATYPE;