【问题标题】:java enum usage getting and setting [closed]java枚举使用获取和设置[关闭]
【发布时间】:2013-02-17 21:17:03
【问题描述】:

我在这里试图在类对象中获取和设置一个枚举,但不知道该怎么做。这就是我到目前为止所拥有的。我已经查阅了所有可能的例子,有些例子似乎太复杂了,我无法理解。请问有什么帮助吗?

public class EnumExample {

public static class Task {

    private String _task;

    public enum Priority {

        ZERO (0), MAYBE (1), LOW (2), MEDIUM (3), HIGH (4), EXTREME (5);

        private int _priority;

        Priority() {
            _priority = 0;
            // Does this set set the default priority level to 0??
        }

        Priority(int priority) {
            _priority = priority;
            // This is where I can set the priority level of this task??
        }

        public int getPriority() {
            return _priority;
        }

        public void setPriority(int priority) {
            _priority = priority;
        }
    }
}



public static void main(String[] args) {

    Task task = new Task();
    task._task = "Study for test";
    System.out.println(task._task);
    System.out.println(task.getPriority());

    // How do I set the priority level for task "study for test"??
    // task.Priority = task.Priority.EXTREME;

    // How do I retrieve the value of priority??
    // System.out.println(task._task.getPriority());

}

}

【问题讨论】:

  • 你甚至还没有为你的类中的枚举类型定义一个字段。
  • 您必须在 stackoverflow 上提出具体问题。 “有什么帮助吗?”不是很具体。
  • 好的 Rohit 和 Katja,你的 2 cmets 帮助我走得更远。我在课堂上为我的枚举类型定义了一个字段。现在我正在尝试获取和设置该字段。不知道该怎么做

标签: java enums


【解决方案1】:

您可以使用枚举的ordinal() 方法来获取其位置。然后,您可以使用它来获取值优先级。

public class Task {

    private Priority priority = Priority.ZERO; // Default priority
    private String name = "";

    public enum Priority {
        ZERO, MAYBE, LOW, MEDIUM, HIGH, EXTREME;
    }

    public Task(String name) {
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
       return name;
    }

    public void setPriority(Priority p) {
        this.priority = p;
    }

    public Priority getPriority() {
        return priority
    }

    public static void main(String[] args) {
        Task t = new Task("Washing up");
        t.setPriority(Priority.HIGH);
        System.out.println(t.getName()); // Washing up
        System.out.println(t.getPriority().toString()); // This gets the string of HIGH
        System.out.println(t.getPriority().ordinal()); // this gives 4
    }
}

【讨论】:

  • 很好的回应!谢谢!
【解决方案2】:

你已经在你的类中定义了一个名为 Priority 的枚举类型,但是为了让你的任务有一个优先级,你必须在你的类中有一个 Priority 类型的成员变量:

public static class Task {

    private String task;

    private Priority priority;

然后您只需通过设置成员来使用您的任务,例如使用 setter 方法:

    public Priority getPriority() {
        return priority;
    }

    public Priority setPriority(Priority priority) {
        this.priority = priority;
    }

然后在你的 main 方法中:

 task.setPriority(Priority.LOW);

关于枚举中的 getter/setter,由于 Priority 类型的语义是表示某个固定的优先级,因此您不应该有 setter。您可能还可以摆脱变量本身(因此也摆脱 getter),因为枚举本身很好地代表了任务的优先级(并且默认情况下枚举已经具有序数)。

因此枚举声明可以很简单:

public enum Priority {
        ZERO, MAYBE, LOW, MEDIUM, HIGH, EXTREME;
}

注意:按照标准 Java 代码样式,您不应该将下划线作为成员的开头。改用驼峰式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    相关资源
    最近更新 更多