public class TestEnum {
/**
* @param args
*/
public static void main(String[] args) {
Color col = Color.RED;
System.out.println("a " + col);// 调用父类的toString()
}
}
enum Color {
// 所有的枚举值都是public static final
RED(255, 0, 0), BLUE(0, 0, 255), BLACK(0, 0, 0), YELLOW(255, 255, 0), GREEN(
0, 255, 0);
// 一个私有的构造函数
private Color(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
// 重写父类的方法
@Override
public String toString() {
return super.toString() + "(" + a + "," + b + "," + c + ")";
}
private int a;
private int b;
private int c;
/*
* Color.RED.ordinal(); //返回结果:0 // 返回枚举值在枚举类种的顺序
* Color.RED.compareTo(Color.BLUE); //返回结果 -1 //可以比较象与指定对象的顺序 values()方法:
* 静态方法,返回一个包含全部枚举值的数组 toString()方法: 返回枚举常量的名称 valueOf()方法:
* 这个方法和toString方法是相对应的,返回带指定名称的指定枚举类型的枚举常量 equals()方法: 比较两个枚举类对象的引用。
*/
}
/**
* @param args
*/
public static void main(String[] args) {
Color col = Color.RED;
System.out.println("a " + col);// 调用父类的toString()
}
}
enum Color {
// 所有的枚举值都是public static final
RED(255, 0, 0), BLUE(0, 0, 255), BLACK(0, 0, 0), YELLOW(255, 255, 0), GREEN(
0, 255, 0);
// 一个私有的构造函数
private Color(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
// 重写父类的方法
@Override
public String toString() {
return super.toString() + "(" + a + "," + b + "," + c + ")";
}
private int a;
private int b;
private int c;
/*
* Color.RED.ordinal(); //返回结果:0 // 返回枚举值在枚举类种的顺序
* Color.RED.compareTo(Color.BLUE); //返回结果 -1 //可以比较象与指定对象的顺序 values()方法:
* 静态方法,返回一个包含全部枚举值的数组 toString()方法: 返回枚举常量的名称 valueOf()方法:
* 这个方法和toString方法是相对应的,返回带指定名称的指定枚举类型的枚举常量 equals()方法: 比较两个枚举类对象的引用。
*/
}