是否可以将数值赋给Java中的枚举?

时间:2023-01-26 14:57:24

Is anything like this possible in Java? Can one assign custom numeric values to enum elements in Java?

Java中有可能是这样的吗?可以为Java中的枚举元素分配自定义数值吗?

public enum EXIT_CODE {
    A=104, B=203;
}

5 个解决方案

#1


154  

public enum EXIT_CODE {
    A(104), B(203);

    private int numVal;

    EXIT_CODE(int numVal) {
        this.numVal = numVal;
    }

    public int getNumVal() {
        return numVal;
    }
}

#2


28  

Yes, and then some, example from documentation:

是的,然后是文档中的一些示例:

public enum Planet {
    MERCURY (3.303e+23, 2.4397e6),
    VENUS   (4.869e+24, 6.0518e6),
    EARTH   (5.976e+24, 6.37814e6),
    MARS    (6.421e+23, 3.3972e6),
    JUPITER (1.9e+27,   7.1492e7),
    SATURN  (5.688e+26, 6.0268e7),
    URANUS  (8.686e+25, 2.5559e7),
    NEPTUNE (1.024e+26, 2.4746e7);

    // in kilograms
    private final double mass;
    // in meters
    private final double radius;
    Planet(double mass, double radius) {
        this.mass = mass;
        this.radius = radius;
    }
    private double mass() { return mass; }
    private double radius() { return radius; }

    // universal gravitational 
    // constant  (m3 kg-1 s-2)
    public static final double G = 6.67300E-11;

    double surfaceGravity() {
        return G * mass / (radius * radius);
    }
    double surfaceWeight(double otherMass) {
        return otherMass * surfaceGravity();
    }
    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Usage: java Planet <earth_weight>");
            System.exit(-1);
        }
        double earthWeight = Double.parseDouble(args[0]);
        double mass = earthWeight/EARTH.surfaceGravity();
        for (Planet p : Planet.values())
           System.out.printf("Your weight on %s is %f%n",
                             p, p.surfaceWeight(mass));
    }
}

#3


10  

Assuming that EXIT_CODE is referring to System . exit ( exit_code ) then you could do

假设EXIT_CODE指的是System。退出(exit_code)然后你可以做

enum ExitCode
{
      NORMAL_SHUTDOWN ( 0 ) , EMERGENCY_SHUTDOWN ( 10 ) , OUT_OF_MEMORY ( 20 ) , WHATEVER ( 30 ) ;

      private int value ;

      ExitCode ( int value )
      {
           this . value = value ;
      }

      public void exit ( )
      {
            System . exit ( value ) ;
      }
}

Then you can put the following at appropriate spots in your code

然后,您可以将以下内容放在代码中的适当位置

ExitCode . NORMAL_SHUTDOWN . exit ( ) '

ExitCode。 NORMAL_SHUTDOWN。出口 ( ) '

#4


2  

Extending Bhesh Gurung's answer for assigning values, you can add explicit method to set value

扩展Bhesh Gurung的分配值的答案,您可以添加显式方法来设置值

   public ExitCode setValue( int value){
      //  A(104), B(203);
      switch(value){
        case 104: return ExitCode.A;
        case 203: return ExitCode.B;
        default:
                   return ExitCode.Unknown //Keep an default or error enum handy
      }
   }

From calling application

从调用应用程序

int i = 104; 
ExitCode serverExitCode = ExitCode.setValue(i);

//You've valid enum from now

//你从现在开始有效的枚举

[Unable to comment to his answer, hence posting it separately]

[无法评论他的回答,因此单独发布]

#5


0  

If you're looking for a way to group constants in a class, you can use a static inner class:

如果您正在寻找一种在类中对常量进行分组的方法,则可以使用静态内部类:

public class OuterClass {
    public void exit(boolean isTrue){
        if(isTrue){
            System.exit(ExitCode.A);
        }else{
            System.exit(ExitCode.B);
        }
    }
    public static class ExitCode{
        public static final int A = 203;
        public static final int B = 204;
    }   
}

#1


154  

public enum EXIT_CODE {
    A(104), B(203);

    private int numVal;

    EXIT_CODE(int numVal) {
        this.numVal = numVal;
    }

    public int getNumVal() {
        return numVal;
    }
}

#2


28  

Yes, and then some, example from documentation:

是的,然后是文档中的一些示例:

public enum Planet {
    MERCURY (3.303e+23, 2.4397e6),
    VENUS   (4.869e+24, 6.0518e6),
    EARTH   (5.976e+24, 6.37814e6),
    MARS    (6.421e+23, 3.3972e6),
    JUPITER (1.9e+27,   7.1492e7),
    SATURN  (5.688e+26, 6.0268e7),
    URANUS  (8.686e+25, 2.5559e7),
    NEPTUNE (1.024e+26, 2.4746e7);

    // in kilograms
    private final double mass;
    // in meters
    private final double radius;
    Planet(double mass, double radius) {
        this.mass = mass;
        this.radius = radius;
    }
    private double mass() { return mass; }
    private double radius() { return radius; }

    // universal gravitational 
    // constant  (m3 kg-1 s-2)
    public static final double G = 6.67300E-11;

    double surfaceGravity() {
        return G * mass / (radius * radius);
    }
    double surfaceWeight(double otherMass) {
        return otherMass * surfaceGravity();
    }
    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Usage: java Planet <earth_weight>");
            System.exit(-1);
        }
        double earthWeight = Double.parseDouble(args[0]);
        double mass = earthWeight/EARTH.surfaceGravity();
        for (Planet p : Planet.values())
           System.out.printf("Your weight on %s is %f%n",
                             p, p.surfaceWeight(mass));
    }
}

#3


10  

Assuming that EXIT_CODE is referring to System . exit ( exit_code ) then you could do

假设EXIT_CODE指的是System。退出(exit_code)然后你可以做

enum ExitCode
{
      NORMAL_SHUTDOWN ( 0 ) , EMERGENCY_SHUTDOWN ( 10 ) , OUT_OF_MEMORY ( 20 ) , WHATEVER ( 30 ) ;

      private int value ;

      ExitCode ( int value )
      {
           this . value = value ;
      }

      public void exit ( )
      {
            System . exit ( value ) ;
      }
}

Then you can put the following at appropriate spots in your code

然后,您可以将以下内容放在代码中的适当位置

ExitCode . NORMAL_SHUTDOWN . exit ( ) '

ExitCode。 NORMAL_SHUTDOWN。出口 ( ) '

#4


2  

Extending Bhesh Gurung's answer for assigning values, you can add explicit method to set value

扩展Bhesh Gurung的分配值的答案,您可以添加显式方法来设置值

   public ExitCode setValue( int value){
      //  A(104), B(203);
      switch(value){
        case 104: return ExitCode.A;
        case 203: return ExitCode.B;
        default:
                   return ExitCode.Unknown //Keep an default or error enum handy
      }
   }

From calling application

从调用应用程序

int i = 104; 
ExitCode serverExitCode = ExitCode.setValue(i);

//You've valid enum from now

//你从现在开始有效的枚举

[Unable to comment to his answer, hence posting it separately]

[无法评论他的回答,因此单独发布]

#5


0  

If you're looking for a way to group constants in a class, you can use a static inner class:

如果您正在寻找一种在类中对常量进行分组的方法,则可以使用静态内部类:

public class OuterClass {
    public void exit(boolean isTrue){
        if(isTrue){
            System.exit(ExitCode.A);
        }else{
            System.exit(ExitCode.B);
        }
    }
    public static class ExitCode{
        public static final int A = 203;
        public static final int B = 204;
    }   
}