Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- public class EnumTutorial{
- // An enum(eration) is a special kind of class that represents a group of constants.
- // You can use enumerations when you would otherwise create multiple classes with a lot of overlapping logic.
- // Instead of creating a class North, East, South and West, you can create a single enumeration with those values:
- enum Direction {
- North, East, South, West;
- }
- // You can access the values with Direction.North, etc.
- // Currently, this enumeration would be equal to four empty classes with the names North, East, etc. Not so useful
- // Let's give it some content, like change in direction in the x and y axes, using attributes:
- enum Direction2 {
- North(0, 1, "Naughty"),
- East(1, 0, "Elephants"),
- South(0, -1, "Spray"),
- West(-1, 0, "Water"); // define values between commas, ending with a semicolon
- public int dX, dY;
- public String mnemonic;
- Direction2(int dX, int dY, String mnemonic) {
- this.dX = dX;
- this.dY = dY;
- this.mnemonic = mnemonic;
- }
- @Override
- public String toString() {
- return mnemonic;
- }
- }
- // Now we have added three attributes and a constructor to the enumeration. This means that each value that is
- // defined in the enumeration has these three attributes (as if we added these attributes to each of the four classes)
- // We need to define the values according to the signature of the constructor. Every Direction2 must have a dX and
- // dY and a mnemonic so we can remember the order (very important). So we create our values as if we are making
- // instances of Direction2, giving the parameters.
- // Let's get more advanced.
- interface Guidable{
- boolean goesNorth();
- boolean horizontallyEqual();
- }
- enum Direction3 implements Guidable{
- North(0,1, "Naughty"){
- @Override
- public boolean horizontallyEqual() { return false; }
- },
- East(1, 0, "Elephants"){
- @Override
- public boolean horizontallyEqual() { return true; }
- },
- South(0, -1, "Spray"){
- @Override
- public boolean horizontallyEqual() { return false; }
- },
- West(-1, 0, "Water"){
- @Override
- public boolean horizontallyEqual() { return true; }
- };
- public int dX, dY;
- public String mnemonic;
- Direction3(int dX, int dY, String mnemonic) {
- this.dX = dX;
- this.dY = dY;
- this.mnemonic = mnemonic;
- }
- @Override
- public String toString() {
- return mnemonic;
- }
- @Override
- public boolean goesNorth() {
- return dY == 1;
- }
- // An enum can implement an interface. Each value in this enum must have the methods of that interface
- // implemented. The methods of this interface can either be implemented for all the values together
- // (like goesNorth) or differently for each value of the enumeration (like horizontallyEqual).
- }
- public static void main(String[] args){
- // Let's use our enum!
- // Note that we don't have to make an instance of the enum. The values inside the enum are implicitly
- // instantiated when we define them using the constructor (which is private).
- for(Direction3 value : Direction3.values()){ // Loop through all values of an enum easily
- System.out.println(value);
- System.out.println(value.dX - value.dY);
- }
- // OUTPUT:
- // Naughty
- // -1
- // Elephants
- // 1
- // Spray
- // 1
- // Water
- // -1
- System.out.println(Direction2.North); // Select enum values seperately with EnumName.ValueName
- // OUTPUT:
- // Naughty
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement