本帖最后由 ㄗ灬Night|K 于 2013-10-9 20:58 编辑
程序第8行,对象怎么直接调用了变量time ??? 不是很明白,在之前的学习中没出现这种情形吧。。。。- package com.jemsn.weekday;
-
- public class test {
-
- public static void main(String[] args) {
- TrafficLamp red=TrafficLamp.Red;
- System.out.println(red.NextLamp());
- System.out.println("this time is "+red.Time);
- }
-
- public enum TrafficLamp{
- Red(30){
- public TrafficLamp NextLamp() {
- return GREEN;
- }
- },
- GREEN(45){
- public TrafficLamp NextLamp() {
- return YELLOW;
- }
- },
- YELLOW(5){
- public TrafficLamp NextLamp() {
- return Red;
- }
- };
- //抽象方法
- public abstract TrafficLamp NextLamp();
- private int Time;
-
- //枚举的有参构造函数
- private TrafficLamp(int time) {
- this.Time=time;
- }
- }
- }
复制代码 |
|