//声明接口
public interface OrderPay {
void pay();
}
//实现接口
public class AliOrderPay implements OrderPay {
@Override
public void pay() {
System.out.println("支付宝支付");
}
}
//实现接口
public class WeixinOrderPay implements OrderPay {
@Override
public void pay() {
System.out.println("微信支付");
}
}
//编写枚举
public enum OrderTypeEnum {
ALIPAY("支付宝", 1,new AliOrderPay()),
WEIPAY("微信", 2,new WeixinOrderPay()),
/** 描述 */
private String desc;
/** 枚举值 */
private int value;
private OrderPay orderPay;
private OrderTypeEnum(String desc, int value, OrderPay orderPay)
{
this.desc = desc;
this.value = value;
this.orderPay = orderPay;
}
public int getValue()
{
return value;
}
public void setValue(int value)
{
this.value = value;
}
public String getDesc()
{
return desc;
}
public void setDesc(String desc)
{
this.desc = desc;
}
public OrderPay getOrderPay() {
return orderPay;
}
public void setOrderPay(OrderPay strategy) {
this.orderPay = strategy;
}
public static OrderTypeEnum getEnum(int value)
{
OrderTypeEnum resultEnum = null;
OrderTypeEnum[] enumAry = OrderTypeEnum.values();
for (int i = 0; i < enumAry.length; i++)
{
if (enumAry[i].getValue() == value)
{
resultEnum = enumAry[i];
break;
}
}
return resultEnum;
}
public static Map<String, Map<String, Object>> toMap()
{
OrderTypeEnum[] ary = OrderTypeEnum.values();
Map<String, Map<String, Object>> enumMap = new HashMap<String, Map<String, Object>>();
for (int num = 0; num < ary.length; num++)
{
Map<String, Object> map = new HashMap<String, Object>();
String key = String.valueOf(getEnum(ary[num].getValue()));
map.put("desc", ary[num].getDesc());
map.put("value", ary[num].getValue());
map.put("orderPay", ary[num].getOrderPay());
enumMap.put(key, map);
}
return enumMap;
}
public static List<Map<String, Object>> toList()
{
OrderTypeEnum[] ary = OrderTypeEnum.values();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < ary.length; i++)
{
Map<String, Object> map = new HashMap<String, Object>();
map.put("desc", ary[i].getDesc());
map.put("value", ary[i].getValue());
map.put("orderPay", ary[i].getOrderPay());
list.add(map);
}
return list;
}
}
//调用
OrderTypeEnum.getEnum(OrderTypeEnum.ALIPAY.getValue()).getOrderPay().pay();
OrderTypeEnum.getEnum(OrderTypeEnum.WEIPAY.getValue()).getOrderPay().pay();