290 votes

Comment convertir la valeur d'un enum en int ?

J'ai une fonction qui retourne un type int. Cependant, je n'ai qu'une valeur de l'énumération TAX.

Comment puis-je convertir la valeur de l'énumération TAX en un int ?

public enum TAX {
    NOTAX(0),SALESTAX(10),IMPORTEDTAX(5);

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

TAX var = TAX.NOTAX; // This value will differ

public int getTaxValue()
{
  // what do do here?
  // return (int)var;
}

1 votes

3voto

public enum ProfileType {
PRIVATE,
PUBLIC
}

private ProfileType profileType = ProfileType.PRIVATE;

Si vous voulez obtenir int de profileType, vous pouvez simplement faire :

int profileTypeInt = profileType.ordinal();

1voto

Ieshaan Saxena Points 19
public enum Tax {

NONE(1), SALES(2), IMPORT(3);

private final int value;
    private Tax(int value) {
        this.value = value;
    }

    public String toString() {
        return Integer.toString(value);
    }
}

class Test {
    System.out.println(Tax.NONE);    //Just an example.
}

1voto

mir Points 51

Une approche quelque peu différente (du moins sur Android) consiste à utiliser la fonction IntDef pour combiner un ensemble de constantes int.

@IntDef({NOTAX, SALESTAX, IMPORTEDTAX})
@interface TAX {}
int NOTAX = 0;
int SALESTAX = 10;
int IMPORTEDTAX = 5;

Utiliser comme paramètre de fonction :

void computeTax(@TAX int taxPercentage){...} 

ou dans une déclaration de variable :

@TAX int currentTax = IMPORTEDTAX;

-1voto

paiego Points 1781

Il est parfois utile de stocker les valeurs de l'enum dans une carte statique :

public static HashMap<NotificationTypes, Integer> getIntValueMap()
{
    if (null == st_map)
    {
        st_map = new HashMap<NotificationTypes, Integer>();

        st_map.put(NotificationTypes.NT_MemberSentMessage, 0);
        st_map.put(NotificationTypes.NT_NewFollower, 1);
        st_map.put(NotificationTypes.NT_LikeSomethingNearby, 2);
        st_map.put(NotificationTypes.NT_MemberLikesCreatorsExp, 3);
        st_map.put(NotificationTypes.NT_MemberLikesPlacePostersExp, 4);
        st_map.put(NotificationTypes.NT_MemberCreatesExpAtPlacePoster, 5);
        st_map.put(NotificationTypes.NT_FollowerHasPosted,  6);
        st_map.put(NotificationTypes.NT_FollowerHasLiked, 7);
        st_map.put(NotificationTypes.NT_NearbyHasJoined, 8); 
        st_map.put(NotificationTypes.NT_NearbyHasPosted, 9);
        st_map.put(NotificationTypes.NT_NearbyHasLiked, 10);
        st_map.put(NotificationTypes.NT_NewClientAvailable, 11);
        st_map.put(NotificationTypes.NT_UnreadMessageUpdate, 12);
        st_map.put(NotificationTypes.NT_MemberStatusIncreased, 13);
        st_map.put(NotificationTypes.NT_BulkFollowers,  14);
        st_map.put(NotificationTypes.NT_BulkFollowersExpert, 15);
        st_map.put(NotificationTypes.NT_DefaultFollowing, 16);
        st_map.put(NotificationTypes.NT_PromptToAddExperience, 17);
        st_map.put(NotificationTypes.NT_MemberCommentedMembersExp,  18);
        st_map.put(NotificationTypes.NT_MemberCommentedOnMembersComment, 19);   
    }

    return st_map;
}

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X