我正在关注这本书,并且有一个枚举示例。所以我这样做:
enum Color {
GREEN("GREEN"),
YELLOW("YELLOW"),
RED("RED");
String name;
Color(String name) {
this.name = name;
}
public String getColor() {
return name;
}
}
然后我想创建对象并通过我的类返回颜色,如下所示:
class TrafficLight extends enum<Color> { // I am getting an error:
// Classes cannot directly extend java.lang.Enum
// ...create objects and etc.
}
我该如何解决这个错误?因为这与我书中的语法完全相同,并且不知道如何使其正确。
编辑:我的书语法:
enum Suit {
CLUBS("CLUBS"),
DIAMONDS("DIAMONDS"),
HEARTS("HEARTS"),
SPADES("SPADES");
String name;
Suit(String name) { this.name = name; }
public String toString() { return name; }
}
class Suit extends Enum<Suit> // pseudo-code only
implements Comparable<Suit>, Serializable {
public static final Suit CLUBS = new Suit("CLUBS");
public static final Suit DIAMONDS = new Suit("DIAMONDS");
public static final Suit HEARTS = new Suit("HEARTS");
public static final Suit SPADES = new Suit("SPADES");
String name;
Suit(String name) { this.name = name; }
public String toString() { return name; }
// ... compiler generated methods ...
}