集册 Java实例教程 将主体用于枚举常量

将主体用于枚举常量

欢马劈雪     最近更新时间:2020-01-02 10:19:05

627
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
将主体用于枚举常量

public class Main {/**来自 nowjava.com - 时代Java**/

  public static void main(String[] args) {

    for (Level s : Level.values()) {

      String name = s.name();

      String desc = s.toString();

      int ordinal = s.ordinal();

      int projectedTurnaroundDays = s.getValue();

      double projectedCost = s.getProjectedCost();

      System.out.println("name=" + name + ", description=" + desc

          + ", ordinal=" + ordinal + ", turnaround days="

          + projectedTurnaroundDays + ", projected cost=" + projectedCost);

    }

  }

}


enum Level {

  LOW("Low Priority", 30) {

    public double getProjectedCost() {

      return 1000.0;

    }

  },

  MEDIUM("Medium Priority", 15) {/*时 代      J a v a   公   众 号 - nowjava.com 提 供*/

    public double getProjectedCost() {

      return 2000.0;

    }

  },

  HIGH("High Priority", 7) {

    public double getProjectedCost() {

      return 3000.0;

    }

  },

  URGENT("Urgent Priority", 1) {

    public double getProjectedCost() {

      return 5000.0;

    }

  };


  // Declare instance variables

  private String description;

  private int value;


  // Declare a private constructor

  private Level(String description, int a) {

    this.description = description;

    this.value = a;

  }


  // Declare a public method to get the turn around days

  public int getValue() {

    return value;

  }


  // Override the toString() method in the Enum class to return description

  @Override

  
展开阅读全文