如何实现枚举类型的某些方法

时间:2015-12-29 19:47:39

标签: java methods types enums enumeration

我是编程和Java的新手,目前正在学习枚举类型。

我创建了一个枚举类型Card,它将每张卡的值存储在一副牌中,例如两个= 2,三个= 3 ... Ace = 11.每个图片卡具有相同的值,10。

我正在尝试实现一个方法getPrevious(),它将返回列表中的先前枚举值。例如,如果在SIX上调用该方法,则会返回FIVE,并且在TWO上调用时将返回ACE。但是我正在努力找到一种方法。

下面你可以看到我写的当前代码,任何有关如何实现方法getPrevious()的帮助或提示都会非常有帮助。

public class EnumPractice {

    public enum Card {
        TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), 
        NINE(9), TEN(10), JACK(10), QUEEN(10), KING(10), ACE(11), ;

        private int value;
        private Card card;

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

        public int getValue(){
            return value;
        }

        public String toString(){
            return "The value of this card is: " + value;
        }
    }

    public static void main(String[] args) {
        System.out.println(Card.ACE.getValue());
    }
}

3 个答案:

答案 0 :(得分:2)

您的getPrevious()方法可能如下所示

public enum Card {
    TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8),
    NINE(9), TEN(10), JACK(10), QUEEN(10), KING(10), ACE(11);

    private int value;

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

    public int getValue() {
        return value;
    }

    public Card getPrevious() {
        int previous = ordinal() - 1;
        if (previous < 0 ) { // if negative, then we have to go to the last element
            previous += values().length;
        }

        return Card.values()[previous];
    }
}

答案 1 :(得分:1)

这是您的getPrevious()实施

import java.util.Arrays;

public class EnumPractice {

    public enum Card {

        TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), 
        NINE(9), TEN(10), JACK(10), QUEEN(10), KING(10), ACE(11), ;

        private int value;
        private Card card;

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

        public int getValue(){
            return value;
        }

        public String toString(){
            return "The value of this card is: " + value;
        }

        public Card getPrevious() {
            int pos = ordinal();
            if (pos == 0)
                return ACE;
            return values()[pos - 1];
        }
    }

    public static void main(String[] args) {

    System.out.println(Card.THREE.getPrevious());
    System.out.println(Card.TWO.getPrevious());

    }

}

以上程序输出

The value of this card is: 2
The value of this card is: 11

答案 2 :(得分:1)

其他方法可能是持有前一张卡的参考。您可以在静态块中创建所有卡片后设置它,如:

ERRORS:
auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with
reverse accessor for 'BaseUser.groups'.
        HINT: Add or change a related_name argument to the definition for 'User.
groups' or 'BaseUser.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permis
sions' clashes with reverse accessor for 'BaseUser.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'User.
user_permissions' or 'BaseUser.user_permissions'.
auth_test.BaseUser.groups: (fields.E304) Reverse accessor for 'BaseUser.groups'
clashes with reverse accessor for 'User.groups'.
        HINT: Add or change a related_name argument to the definition for 'BaseU
ser.groups' or 'User.groups'.
auth_test.BaseUser.user_permissions: (fields.E304) Reverse accessor for 'BaseUse
r.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'BaseU
ser.user_permissions' or 'User.user_permissions'.