java enum getter setter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29430182/
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-08-11 07:58:53  来源:igfitidea点击:

java enum getter setter

javaenumssettergetter-setter

提问by GAMA

I'm consuming data from web service, will be storing that data into data holder classes and then get that data at some other place.

我正在使用来自 Web 服务的数据,将该数据存储到数据持有者类中,然后在其他地方获取该数据。

For a particular field, most suitable data type is enum and hence I've created following enum:

对于特定字段,最合适的数据类型是枚举,因此我创建了以下枚举:

public enum EventStatus {
    PENDING(1), OPEN(2), DISPATCHED(3), APPENDED(4), CLOSED(5), REQUESTED_TO_CLOSE(
            6), ACTION_REQUESTED_FROM_POLICE_STATION(7), ACTION_REQUESTED_FROMD_ISPATCHER(
            8), ACTION_REQUESTED_FROM_SUPERVISOR(9);
    private int value;

    private EventStatus(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
    //Just for testing from some SO answers, but no use
    public void setValue(int value) {
        this.value = value;
    }
}

This enum is used within another class as follows:

此枚举在另一个类中使用,如下所示:

public EventStatus getEventStatus() {
    return eventStatus;
}

public void setEventStatus(EventStatus eventStatus) {
    this.eventStatus = eventStatus;
}

Now when I try to set value like following:

现在,当我尝试设置如下值时:

event.setEventStatus(getAttributeValueInt(linkedEventElement, "status"));

which equivalents to

相当于

event.setEventStatus(1);

I'm getting compilation error that method is not applicable for arguments(int)

我收到编译错误,该方法不适用于 arguments(int)

One way I can do this is by something like this:

我可以做到这一点的一种方法是这样的:

switch(getAttributeValueInt(linkedEventElement, "status")){
   case 1:eventLinkedEvent.setEventStatus(EventStatus.PENDING); 
      //and so on...
}

But this ruins the sole purpose of enum.

但这破坏了枚举的唯一目的。

Also how to get event status value in integer form?

另外如何以整数形式获取事件状态值?

Can anyone please guide me how to go ahead?

谁能指导我如何继续?

采纳答案by Akash Chavda

You can create enum like this

您可以像这样创建枚举

public enum EventStatus {
    PENDING(1), OPEN(2), DISPATCHED(3), APPENDED(4), CLOSED(5), REQUESTED_TO_CLOSE(
            6), ACTION_REQUESTED_FROM_POLICE_STATION(7), ACTION_REQUESTED_FROMD_ISPATCHER(
            8), ACTION_REQUESTED_FROM_SUPERVISOR(9);
    private int value;

    private EventStatus(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
    //Just for testing from some SO answers, but no use
    public void setValue(int value) {
        this.value = value;
    }

    public static EventStatus getEventStatusById(int id) {

        EventStatus event = null;

        switch (id) {
        case 1:
            event = PENDING;
            break;
        case 2:
            event = OPEN;
            break;
        case 3:
            event = DISPATCHED; 
            break;
        case 4:
            event = APPENDED;
            break;
        case 5:
            event = CLOSED;
            break;
        case 6:
            event = REQUESTED_TO_CLOSE;
            break;
        case 7:
            event = ACTION_REQUESTED_FROM_POLICE_STATION;
            break;
        case 8:
            event = ACTION_REQUESTED_FROMD_ISPATCHER;
            break;
        case 9:
            event = ACTION_REQUESTED_FROM_SUPERVISOR;
            break;

        default:
            break;
        }
        return event;
    }
}

after you can try below line to set EventStatus

在您可以尝试在下面的行设置 EventStatus 之后

event.setEventStatus(EventStatus.getEventStatusById(getAttributeValueInt(linkedEventElement, "status")));

I think its useful..

我觉得有用。。

回答by Rohit Jain

Create a mapping in enum itself from value to enum constant. And then a staticmethod, which takes an inttype and returns the enumtype.

在枚举本身中创建从值到枚举常量的映射。然后是一个static方法,它接受一个int类型并返回该enum类型。

public enum EventStatus {
    // constants
    ;
    private final static Map<Integer, EventStatus> REVERSE_MAP = new HashMap<>();

    static {
        for (EventStatus status: values()) {
            REVERSE_MAP.put(status.value, status);
        }
    }

    public static EventStatus forValue(int value) {
        return REVERSE_MAP.get(value);
    }
}

回答by JB Nizet

An enum is a class. It's not a simple alias to an int as it is in C for example. 1 is an integer, not an instance of EventStatus, and your method expects an instance of EventStatus, so that doesn't compile.

枚举是一个类。它不是 int 的简单别名,例如在 C 中。1 是一个整数,而不是 EventStatus 的实例,并且您的方法需要一个 EventStatus 实例,因此无法编译。

Just provide a factory method in the enum to transform an int value to the enum:

只需在枚举中提供一个工厂方法即可将 int 值转换为枚举:

public static EventStatus fromIntValue(int value) {
    // iterate through the enum constants, returned by EventStatus.value(), 
    // and find the one with the given value
}

You could also store the enum constants in a Map<Integer, EventStatus>to make the lookup O(1).

您还可以将枚举常量存储在 a 中Map<Integer, EventStatus>以进行查找 O(1)。

回答by Mustafa sabir

Quick solution, since your ordinals (Enum indexes) match your values (with a difference of 1), modify your Enum EventStatusto include new array of EventStatus:-

快速解决方案,因为您的序号(枚举索引)与您的值匹配(相差 1),请修改您的枚举EventStatus以包含新数组EventStatus:-

public enum EventStatus {
    PENDING(1), OPEN(2), DISPATCHED(3), APPENDED(4), CLOSED(5), REQUESTED_TO_CLOSE(
            6), ACTION_REQUESTED_FROM_POLICE_STATION(7), ACTION_REQUESTED_FROMD_ISPATCHER(
            8), ACTION_REQUESTED_FROM_SUPERVISOR(9);
    private int value;

    public static final EventStatus values[] = values();

    private EventStatus(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
    //Just for testing from some SO answers, but no use
    public void setValue(int value) {
        this.value = value;
    }
}

Now instead of this:-

现在而不是这个:-

event.setEventStatus(getAttributeValueInt(linkedEventElement, "status"));

Use this:-

用这个:-

event.setEventStatus(EventStatus.values[(getAttributeValueInt(linkedEventElement,"status"))-1)]);

This will return Enum value for corresponding ordinal value, For example:-

这将返回相应序号值的枚举值,例如:-

EventStatus.values[1-1]; //this will return EnumStatus.PENDING

-1is because you have provided your ordinal values starting from 1.

-1是因为您提供了从 1 开始的序数值。