参考ThinkJava中的例子
整理如下
OutCome
package com.enumDistribute;
public enum OutCome
{
WIN, LOSE, DRAW
}
Competitor
package com.enumDistribute;
public interface Competitor<T extends Competitor<T>>
{
OutCome compete(T competitor);
}
RoShamBo
package com.enumDistribute;
import java.util.Random;
public class RoShamBo
{
public static <T extends Competitor<T>> void match(T a, T b)
{
System.out.println(a + " vs " + b + " : " + a.compete(b));
}
public static <T extends Enum<T> & Competitor<T>> void play(Class<T> rsbClass, int size)
{
for (int i = 0; i < size; i++)
{
match(random(rsbClass), random(rsbClass));
}
}
public static <T extends Enum<T>> T random(Class<T> cls)
{
T[] array = cls.getEnumConstants();
// System.out.println("length : "+array.length);
Random random = new Random();
T t = array[random.nextInt(array.length)];
System.out.println("t : " + t);
return t;
}
}
RoShamBo2
package com.enumDistribute;
import static com.enumDistribute.OutCome.DRAW;
import static com.enumDistribute.OutCome.LOSE;
import static com.enumDistribute.OutCome.WIN;
public enum RoShamBo2 implements Competitor<RoShamBo2>
{
/**
* DRAW, LOSE, WIN参考的是OutCome paper, OutCome sham, OutCome rock
*/
PAPER(DRAW, LOSE, WIN),
SHAM(WIN, DRAW, LOSE),
ROCK(LOSE, WIN, DRAW);
OutCome _paper, _sham, _rock;
RoShamBo2(OutCome paper, OutCome sham, OutCome rock)
{
this._paper = paper;
this._sham = sham;
this._rock = rock;
}
@Override
public OutCome compete(RoShamBo2 it)
{
switch (it)
{
/*
* default 放在上面/枚举中所有情况都可以被列举到。
*
* PAPER、SHAM、ROCK是it的
* _paper、_sham、_rock是this的
*
*/
default:
case PAPER:return _paper;
case SHAM: return _sham;
case ROCK:return _rock;
}
}
public static void main(String[] args)
{
RoShamBo.play(RoShamBo2.class, 3);
}
}
RoShamBo3
package com.enumDistribute;
public enum RoShamBo3 implements Competitor<RoShamBo3>
{
PAPER
{
@Override
public OutCome compete(RoShamBo3 it)
{
switch (it)
{
default :
case PAPER:return OutCome.DRAW;
case SCISSORS :return OutCome.LOSE;
case ROCK :return OutCome.WIN;
}
}
},
SCISSORS
{
@Override
public OutCome compete(RoShamBo3 it)
{
switch (it)
{
default :
case PAPER:return OutCome.WIN;
case SCISSORS :return OutCome.DRAW;
case ROCK :return OutCome.LOSE;
}
}
},
ROCK
{
@Override
public OutCome compete(RoShamBo3 it)
{
switch (it)
{
default :
case PAPER:return OutCome.LOSE;
case SCISSORS :return OutCome.WIN;
case ROCK :return OutCome.DRAW;
}
}
};
public abstract OutCome compete(RoShamBo3 it);
public static void main(String[] args)
{
RoShamBo.play(RoShamBo3.class, 10);
}
}
RoShamBo5
package com.enumDistribute;
import java.util.EnumMap;
public enum RoShamBo5 implements Competitor<RoShamBo5>
{
PAPER, SCISSORS, ROCK;
static EnumMap<RoShamBo5, EnumMap<RoShamBo5, OutCome>> table = new EnumMap<RoShamBo5, EnumMap<RoShamBo5, OutCome>>(RoShamBo5.class);
static
{
for (RoShamBo5 it : RoShamBo5.values())
{
table.put(it, new EnumMap<RoShamBo5, OutCome>(RoShamBo5.class));
}
/**
* DRAW, LOSE, WIN参考的是OutCome _paper, OutCome _scissors, OutCome _rock
*/
initRow(PAPER, OutCome.DRAW, OutCome.LOSE, OutCome.WIN);
initRow(SCISSORS, OutCome.WIN, OutCome.DRAW, OutCome.LOSE);
initRow(ROCK, OutCome.LOSE, OutCome.WIN, OutCome.DRAW);
}
static void initRow(RoShamBo5 it, OutCome _paper, OutCome _scissors, OutCome _rock)
{
EnumMap<RoShamBo5, OutCome> row = RoShamBo5.table.get(it);
row.put(RoShamBo5.PAPER, _paper);
row.put(RoShamBo5.SCISSORS, _scissors);
row.put(RoShamBo5.ROCK, _rock);
}
@Override
public OutCome compete(RoShamBo5 rb5)
{
//this vs it
return table.get(this).get(rb5);
}
public static void main(String[] args)
{
RoShamBo.play(RoShamBo5.class, 10);
}
}
RoShamBo6
package com.enumDistribute;
public enum RoShamBo6 implements Competitor<RoShamBo6>
{
PAPER, SCISSORS, ROCK;
private static OutCome[][] table = {
{OutCome.DRAW, OutCome.LOSE, OutCome.WIN},// PAPER
{OutCome.WIN, OutCome.DRAW, OutCome.LOSE},// SCISSORS
{OutCome.LOSE, OutCome.WIN, OutCome.DRAW}// ROCK
};
public static void main(String[] args)
{
RoShamBo.play(RoShamBo6.class, 10);
}
@Override
public OutCome compete(RoShamBo6 other)
{
return table[this.ordinal()][other.ordinal()];
}
}
小结:确定情况的多路判断可以使用enum来改写。