手记

Enum枚举类实现接口,方法的多态设计

public enum ObConstant implements EnumDescription
{
TURN_LEFT
{
@Override
public void description()
{
System.out.println("turn left");
}

    @Override
    public void saySomething()
    {
        // TODO Auto-generated method stub

    }

},
TRUN_RIGHT
{
    @Override
    public void description()
    {
        System.out.println("turn right");
    }

    @Override
    public void saySomething()
    {
        // TODO Auto-generated method stub

    }      
}

}

public interface EnumDescription
{
void description();
int i=0;
void saySomething();
}

public class EnumTest
{
private enum opConstant{TURN_LEFT
,TURN_RIGHT
,SHOT};

public static void doOp(opConstant op)
{
    switch(op)
    {
        case TURN_LEFT:
            System.out.println("left");
            break;
        case TURN_RIGHT:
            System.out.println("right");
            break;
        default:
            System.out.println("shot");
            break;
    }
}

public static void main(String[] args)
{
    System.out.println(opConstant.TURN_LEFT);

    ObConstant.TURN_LEFT.description();
}

}

0人推荐
随时随地看视频
慕课网APP