Diferències
Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.
| Ambdós costats versió prèvia Revisió prèvia Següent revisió | Revisió prèvia | ||
| development:java:enum [03/06/2018 04:14] – [simple] mate | development:java:enum [05/06/2018 03:01] (actual) – [Enum] mate | ||
|---|---|---|---|
| Línia 1: | Línia 1: | ||
| = Enum | = Enum | ||
| + | {{tag> | ||
| == revisar | == revisar | ||
| * [[https:// | * [[https:// | ||
| == ejemplos | == ejemplos | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| === simple | === simple | ||
| <sxh java> | <sxh java> | ||
| Línia 13: | Línia 17: | ||
| } | } | ||
| </ | </ | ||
| - | <sxh> | + | < |
| System.out.println(UserStatus.ACTIVE); | System.out.println(UserStatus.ACTIVE); | ||
| </ | </ | ||
| Línia 43: | Línia 47: | ||
| <sxh java> | <sxh java> | ||
| System.out.println(WhoisRIR.ARIN.url()); | System.out.println(WhoisRIR.ARIN.url()); | ||
| + | </ | ||
| + | === simple + campo + lógica | ||
| + | <sxh java> | ||
| + | public enum Operation { | ||
| + | PLUS, | ||
| + | MINUS, | ||
| + | TIMES, | ||
| + | DIVIDE; | ||
| + | |||
| + | double calculate(double x, double y) { | ||
| + | switch (this) { | ||
| + | case PLUS: | ||
| + | return x + y; | ||
| + | case MINUS: | ||
| + | return x - y; | ||
| + | case TIMES: | ||
| + | return x * y; | ||
| + | case DIVIDE: | ||
| + | return x / y; | ||
| + | default: | ||
| + | throw new AssertionError(" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | <sxh java> | ||
| + | double result = Operation.PLUS.calculate(1, | ||
| + | System.out.println(result); | ||
| + | </ | ||
| + | |||
| + | === bucle | ||
| + | <sxh java> | ||
| + | for (UserStatus status : UserStatus.values()) { | ||
| + | System.out.println(status); | ||
| + | } | ||
| + | </ | ||
| + | <sxh> | ||
| + | PENDING | ||
| + | ACTIVE | ||
| + | INACTIVE | ||
| + | DELETED | ||
| + | </ | ||
| + | |||
| + | === comparación | ||
| + | <sxh java> | ||
| + | WhoisRIR rir = WhoisRIR.APNIC; | ||
| + | |||
| + | if(rir == WhoisRIR.APNIC) { | ||
| + | System.out.println(" | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | === comparacion 2 | ||
| + | <sxh java> | ||
| + | public class Test { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | |||
| + | WhoisRIR rir = WhoisRIR.RIPE; | ||
| + | |||
| + | switch (rir) { | ||
| + | case ARIN: | ||
| + | System.out.println(" | ||
| + | break; | ||
| + | case APNIC: | ||
| + | System.out.println(" | ||
| + | break; | ||
| + | case RIPE: | ||
| + | System.out.println(" | ||
| + | break; | ||
| + | default: | ||
| + | throw new AssertionError(" | ||
| + | |||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | === string to enum | ||
| + | <sxh java> | ||
| + | //enum valueOf + uppercase | ||
| + | Operation op = Operation.valueOf(" | ||
| + | System.out.println(op.calculate(10, | ||
| + | </ | ||
| + | <sxh java> | ||
| + | // Recommended Solution : add locale | ||
| + | WhoisRIR rir = WhoisRIR.valueOf(" | ||
| + | System.out.println(rir); | ||
| + | System.out.println(rir.url()); | ||
| </ | </ | ||