-
Notifications
You must be signed in to change notification settings - Fork 0
2.5 Use enumerated types
Weverton edited this page Aug 29, 2016
·
12 revisions
Lembretes:
- Construtor para enum só é permitido
private; - Um enum herda de java.lang.Enum, sendo assim não pode herdar de outras classes;
- Um enum pode implementar interfaces;
Na label do switch deve ser utilizado o unqualified name no enum.
public class Teste10 {
public static void main(String[] args) {
Switch s = Switch.OFF;
switch (s) {
case Switch.OFF: // Deve ser utilizado somente o OFF
System.out.println("It is off!");
break;
}
}
}
enum Switch {
ON, OFF
}
Exemplo de enum:
enum Title {
MR("Mr. "), MRS("Mrs. "), MS("Ms. ");
private String title;
private Title(String s) {
title = s;
}
public String format(String first, String last) {
return title + " " + first + " " + last;
}
}
Invalido, não pode chamar o método format devido o mesmo não ser static.
class TestClass {
void someMethod() {
System.out.println(Title.format("Rob", "Miller"));
}
}
Isso não pode:
class TestClass {
void someMethod() {
Title.DR dr = new Title.DR("Dr. ");
}
}