- enum TrafficLamp{
- /**
- * 对于这道题目而言,使用的其实是匿名对象内部类!
- * 枚举最简单的创建对象方法:RED,YELLOW,GREEN
- * 对于普通的类而言,创建对象:Person p = new Person();
- *
- */
-
- RED(30){//这是什么意思?
- @Override
- public TrafficLamp nextLamp() {
- return YELLOW;
- }
-
- },
-
- YELLOW(40){
- @Override
- public TrafficLamp nextLamp() {
- return GREEN;
- }
-
- },
-
- GREEN(45){
- @Override
- public TrafficLamp nextLamp() {
- return RED;
- }
-
- };
- public abstract TrafficLamp nextLamp();
- private int time;
- private TrafficLamp(int time){
- this.time = time;
- }
- public int getTime() {
- return time;
- }
- public void setTime(int time) {
- this.time = time;
- }
-
- }
- public class EnumDemo4 {
- public static void main(String[] args) {
- System.out.println(TrafficLamp.RED.nextLamp());
- System.out.println(TrafficLamp.RED.nextLamp().getTime());
-
- }
- }
复制代码
谁能帮我理理枚举的使用?像这道题我就没看懂,谁能帮帮忙讲解一下? |
|