Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save luisartola/3052925 to your computer and use it in GitHub Desktop.
Save luisartola/3052925 to your computer and use it in GitHub Desktop.
Java Enums - ¡Inmutable by default!
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);
}
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);
}
}
for (Planet p : Planet.values()){
p.tralari()
}
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