Advertisement
tvdhout

Enums: why and how

May 15th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 KB | None | 0 0
  1. package main;
  2.  
  3. public class EnumTutorial{
  4.     // An enum(eration) is a special kind of class that represents a group of constants.
  5.     // You can use enumerations when you would otherwise create multiple classes with a lot of overlapping logic.
  6.     // Instead of creating a class North, East, South and West, you can create a single enumeration with those values:
  7.  
  8.     enum Direction {
  9.         North, East, South, West;
  10.     }
  11.  
  12.     // You can access the values with Direction.North, etc.
  13.     // Currently, this enumeration would be equal to four empty classes with the names North, East, etc. Not so useful
  14.     // Let's give it some content, like change in direction in the x and y axes, using attributes:
  15.  
  16.     enum Direction2 {
  17.         North(0, 1, "Naughty"),
  18.         East(1, 0, "Elephants"),
  19.         South(0, -1, "Spray"),
  20.         West(-1, 0, "Water"); // define values between commas, ending with a semicolon
  21.  
  22.         public int dX, dY;
  23.         public String mnemonic;
  24.         Direction2(int dX, int dY, String mnemonic) {
  25.             this.dX = dX;
  26.             this.dY = dY;
  27.             this.mnemonic = mnemonic;
  28.         }
  29.  
  30.         @Override
  31.         public String toString() {
  32.             return mnemonic;
  33.         }
  34.     }
  35.  
  36.     // Now we have added three attributes and a constructor to the enumeration. This means that each value that is
  37.     // defined in the enumeration has these three attributes (as if we added these attributes to each of the four classes)
  38.     // We need to define the values according to the signature of the constructor. Every Direction2 must have a dX and
  39.     // dY and a mnemonic so we can remember the order (very important). So we create our values as if we are making
  40.     // instances of Direction2, giving the parameters.
  41.  
  42.     // Let's get more advanced.
  43.  
  44.     interface Guidable{
  45.         boolean goesNorth();
  46.         boolean horizontallyEqual();
  47.     }
  48.  
  49.     enum Direction3 implements Guidable{
  50.         North(0,1, "Naughty"){
  51.             @Override
  52.             public boolean horizontallyEqual() { return false; }
  53.         },
  54.         East(1, 0, "Elephants"){
  55.             @Override
  56.             public boolean horizontallyEqual() { return true; }
  57.         },
  58.         South(0, -1, "Spray"){
  59.             @Override
  60.             public boolean horizontallyEqual() { return false; }
  61.         },
  62.         West(-1, 0, "Water"){
  63.             @Override
  64.             public boolean horizontallyEqual() { return true; }
  65.         };
  66.  
  67.  
  68.         public int dX, dY;
  69.         public String mnemonic;
  70.         Direction3(int dX, int dY, String mnemonic) {
  71.             this.dX = dX;
  72.             this.dY = dY;
  73.             this.mnemonic = mnemonic;
  74.         }
  75.  
  76.         @Override
  77.         public String toString() {
  78.             return mnemonic;
  79.         }
  80.  
  81.         @Override
  82.         public boolean goesNorth() {
  83.             return dY == 1;
  84.         }
  85.  
  86.         // An enum can implement an interface. Each value in this enum must have the methods of that interface
  87.         // implemented. The methods of this interface can either be implemented for all the values together
  88.         // (like goesNorth) or differently for each value of the enumeration (like horizontallyEqual).
  89.     }
  90.  
  91.     public static void main(String[] args){
  92.         // Let's use our enum!
  93.         // Note that we don't have to make an instance of the enum. The values inside the enum are implicitly
  94.         // instantiated when we define them using the constructor (which is private).
  95.         for(Direction3 value : Direction3.values()){ // Loop through all values of an enum easily
  96.             System.out.println(value);
  97.             System.out.println(value.dX - value.dY);
  98.         }
  99.        
  100.         // OUTPUT:
  101.         // Naughty
  102.         // -1
  103.         // Elephants
  104.         // 1
  105.         // Spray
  106.         // 1
  107.         // Water
  108.         // -1
  109.  
  110.         System.out.println(Direction2.North); // Select enum values seperately with EnumName.ValueName
  111.        
  112.         // OUTPUT:
  113.         // Naughty
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement