Created
July 5, 2012 10:45
-
-
Save luisartola/3052925 to your computer and use it in GitHub Desktop.
Java Enums - ¡Inmutable by default!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum Operation { | |
PLUS("+"){ | |
double apply (double x, double y) { return x + y;} | |
}, | |
MINUS("-"){ | |
double apply (double x, double y) { return x - y;} | |
}; | |
private final String symbol; | |
Operation (String symbol){ this.symbol = symbol;} | |
@Override public String toString(){ return symbol;} | |
abstract double apply (double x, double y); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum Options { | |
HELP("tralari", 3), | |
FILE("tralara", 1), | |
URL("pichiflu", 2); | |
private final String nombre; | |
private final int numero; | |
Options(String nombre, int numero) { | |
this.nombre = nombre; | |
this.numero = numero; | |
} | |
public String nombre() { | |
return this.nombre; | |
} | |
public String nombreCompleto() { | |
return this.nombre + String.valueOf(this.numero); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (Planet p : Planet.values()){ | |
p.tralari() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum Color { | |
RED, BLUE, YELLOW, GREEN, ORANGE, PURPLE; | |
public static EnumSet<Color> getPrimaryColors() { | |
return EnumSet.of(RED, BLUE, YELLOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment