jdk1.5新特性5之枚举之模拟枚举类型

本文通过两种方式展示了如何利用枚举类型实现交通信号灯的状态转换。第一种方式直接定义静态成员并实现状态切换;第二种则采用匿名内部类扩展抽象类的方式实现。这两种方法都有效地实现了信号灯状态的管理和转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模拟方式一

package cn.xy.Enum;

public class TrafficLampEasy
{
private int time;

public final static TrafficLampEasy REDLAMP = new TrafficLampEasy(20);
public final static TrafficLampEasy GREENLAMP = new TrafficLampEasy(20);
public final static TrafficLampEasy YELLOWLAMP = new TrafficLampEasy(5);

public TrafficLampEasy()
{
}

public TrafficLampEasy(int time)
{
this.time = time;
}

public TrafficLampEasy nextLamp()
{
TrafficLampEasy result = new TrafficLampEasy();

if (this == REDLAMP)
{
result = GREENLAMP;
}
else if (this == GREENLAMP)
{
result = YELLOWLAMP;
}
else if (this == YELLOWLAMP)
{
result = REDLAMP;
}
return result;
}

public String getValue()
{
String result = "";

if (this == REDLAMP)
{
result = "红灯,时长:" + time;
}
else if (this == GREENLAMP)
{
result = "绿灯,时长:" + time;
}
else if (this == YELLOWLAMP)
{
result = "黄灯,时长:" + time;
}
return result;
}

public int getTime()
{
return time;
}

public void setTime(int time)
{
this.time = time;
}
}

TrafficLampEasy teRed = TrafficLampEasy.REDLAMP;
System.out.println(teRed.getValue());
System.out.println(teRed.nextLamp().getValue());

从这个例子中我们看出,枚举类型其实就是一个类返回该类本身

模拟方式二

package cn.xy.Enum;

public abstract class TrafficLamp
{
/**
* 下一个灯
*/
public abstract TrafficLamp nextLamp();

/**
* 获取值
*/
public abstract String getValue();

/**
* 时长
*/
private int time;

public TrafficLamp()
{
}

public TrafficLamp(int time)
{
this.time = time;
}

/**
* 红灯,匿名类,相当于继承TrafficLamp抽象类,并实现抽象方法
*/
public final static TrafficLamp REDLAMP = new TrafficLamp(50) {
@Override
public TrafficLamp nextLamp()
{
return GREENLAMP;
}

@Override
public String getValue()
{
return "红灯,时长:" + this.getTime();
}
};

public final static TrafficLamp GREENLAMP = new TrafficLamp(50) {
@Override
public TrafficLamp nextLamp()
{
return YELLOWLAMP;
}

@Override
public String getValue()
{
return "绿灯,时长:" + this.getTime();
}
};

public final static TrafficLamp YELLOWLAMP = new TrafficLamp(2) {
@Override
public TrafficLamp nextLamp()
{
return REDLAMP;
}

@Override
public String getValue()
{
return "黄灯,时长:" + this.getTime();
}
};

public int getTime()
{
return time;
}

public void setTime(int time)
{
this.time = time;
}
}

TrafficLamp red = TrafficLamp.REDLAMP;
System.out.println(red.getValue());
System.out.println(red.nextLamp().getValue());

采用匿名类的方式,那么什么是匿名类呢?

匿名类适合那些只需要使用一次的类

public abstract class AnonymousClassDesk
{
public abstract double getPrice();

public abstract String getName();

}

public class Desk extends AnonymousClassDesk
{
@Override
public double getPrice()
{
return 100;
}

@Override
public String getName()
{
return "普通书桌";
}

}

public static void main(String[] args)
{
AnonymousClassDesk desk = new AnonymousClassDesk() {

@Override
public double getPrice()
{
return 100;
}

@Override
public String getName()
{
return "匿名书桌";
}
};

System.out.println(desk.getName());
}

不仅可以使抽象类,也可以是接口。匿名类没有什么特别的地方,同样还是要实现需要实现的方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值