简体   繁体   English

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

[英]How to implement certain method for enum type

I am new to programming and Java and am currently learning enum types. 我是编程和Java的新手,目前正在学习枚举类型。

I have created an enum type Card which stores the value of each card in a deck of cards, for example Two = 2, Three = 3 ... Ace = 11. Each picture card has the same value, 10. 我创建了一个枚举类型的Card ,它将每张卡的值存储在一副纸牌中,例如2 = 2、3 = 3 ... Ace =11。每张图片卡具有相同的值10。

I am trying to implement a method getPrevious() , that will return the previous enum value in the list. 我正在尝试实现方法getPrevious() ,该方法将返回列表中的上一个枚举值。 For example if the method is called on SIX , it will return FIVE , and when called on TWO will return ACE . 例如,如果在SIX上调用了该方法,则它将返回FIVE ,而在TWO被调用时,将返回ACE However i am struggling to figure out a way to do so. 但是,我正在努力寻找一种方法来做到这一点。

Below you can see the current code that i have written, any help or tips on how to implement the method getPrevious() would be incredibly helpful. 在下面,您可以看到我所编写的当前代码,有关如何实现方法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());
    }
}

Your getPrevious() method can look like this 您的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];
    }
}

here is your getPrevious() implemenation 这是您的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 above program output is 上面的程序输出是

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

Other approach could be holding reference to previous card. 其他方法可能是保留对先前卡的引用。 You could set it after all cards are created in static block like: 您可以在所有卡都在静态块中创建后进行设置,例如:

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 previous;

    static{
        //lets set previous element for cards
        //we will iterate starting from TWO so its previous should be:
        Card previous = ACE;//yes ACE
        for (Card card : values()){
            card.previous = previous;
            previous = card; 
        }
    }

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

    public int getValue(){
        return value;
    }

    public Card getPrevious(){
        return previous; //<-- simple getter is enough since we took care 
                         //    of setting this value earlier in static 
                         //    initialization block
    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM