Which is more appropriate for static data - final char or enum?

只愿长相守 提交于 2019-12-08 02:08:08

问题


Relating to my Poker Java program would it be a wise decision to choose enum's over chars and ints?

As far as I can tell assigning a separate integer value to a char has the benefit of ease of mathematical operators being applied when it comes to comparing card values to decide a winner. However this may be possible with enums, if so I'm unaware.

Please can someone explain the adv/disadv of each to me?

My first option of declaring card ranks is as enums:

public enum Rank { 
     DEUCE (1), 
     THREE (2), 
     FOUR (3), 
     FIVE (4), 
     SIX (5),     
     SEVEN (6), 
     EIGHT (7), 
     NINE (8), 
     TEN (9), 
     JACK (10), 
     QUEEN (11), 
     KING (12), 
     ACE (13)
}

public enum Suit { 
     CLUBS (1), 
     DIAMONDS (2), 
     HEARTS (3), 
     SPADES (4) 
}

My second option is as static final chars with assigned int values as such:

       public static final char ACE =   'A';
       public static final char TWO =   '2';
       public static final char THREE = '3';
       public static final char FOUR =  '4';
       public static final char FIVE =  '5';
       public static final char SIX =   '6';
       public static final char SEVEN = '7';
       public static final char EIGHT = '8';
       public static final char NINE =  '9';
       public static final char TEN =   'T';
       public static final char JACK =  'J';
       public static final char QUEEN = 'Q';
       public static final char KING =  'K';

         public Rank (char c)
         {
             switch (c)
             {
             case TWO:
                 _val = 0;
                 break;
             case THREE:
                 _val = 1;
                 break;
             case FOUR: 
                 _val = 2;
                 break;
             case FIVE: 
                 _val = 3;
                 break;
             case SIX:
                 _val = 4;
                 break;
             case SEVEN:
                 _val = 5;
                 break;
             case EIGHT:
                 _val = 6;
                 break;
             case NINE: 
                 _val = 7;
                 break;
             case 'T':
                 _val = 8;
                 break;
             case 'J':  
                 _val = 9;
                 break;
             case 'Q':
                 _val = 10;
                 break;
             case 'K': 
                 _val = 11;
                 break;
             case 'A':
                 _val = 12;
                 break;
             default:
                 _val = -1;
             }
         }

Thanks!


回答1:


I would prefer Enum. Code looks cleaner and IDEs may help you to avoid missing a case in switch statements.




回答2:


I'd suggest you read Item 30: Use enums instead of int constants from Effective Java by Joshua Bloch. It explains the many advantages to using enums over int constants.




回答3:


Use enums. Because of:

  • style - they look better and cleaner
  • IDE integration - the IDE can prompt you for valid input
  • methods can be passed enums which are automatically imbued with range checking (see below)
  • enums can have methods to return values and/or do calculations
  • the data you're dealing with has a definite range of values that won't ever change, so defining an enum instance for each value makes sense

Consider this method:

public static char addCards(char a, char b);

I can call it with invalid input

addCards('x', '$');

There's no range checking built in with char. But with enums, it comes for free.


As for your issue of ranking, with enums you can simply do this

Rank r1, r2;
boolean r1RankedHigherThanR2 = r1.ordinal() > r2.ordinal();

The order you define the enum instances in is enough to convey ranking order.




回答4:


Enums have an ordinal number (0-based). So you can use that to rank them and you probably shouldn't duplicate that. e.g. KING.ordinal().

Also, as an aside, in poker suits don't have a rank - you have your suits in bridge rank order.



来源:https://stackoverflow.com/questions/14246010/which-is-more-appropriate-for-static-data-final-char-or-enum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!