如果enum实现Comparable,那么为什么不能与进行比较?

public class Dream
{
    public static void main(String... args)
    {
        System.out.println(PinSize.BIG == PinSize.BIGGER); //false
        System.out.println(PinSize.BIG == PinSize.BIG); //true
        System.out.println(PinSize.BIG.equals(PinSize.BIGGER));//false
        System.out.println(PinSize.BIG > PinSize.BIGGERER);// compilation error
        //can't be compared
        System.out.println(PinSize.BIG.toString().equals(PinSize.BIGGER));// #4
        PinSize b = PinSize.BIG ;
        System.out.println( b instanceof Comparable);// true
    }
}
enum PinSize { BIG, BIGGER, BIGGERER };

最佳答案

你可以这样做:

PinSize.BIGGEST.ordinal() > PinSize.BIG.ordinal()  // evaluates to `true`

当然,假设在枚举中BIGGEST之后声明了BIG。枚举中的序数值与声明顺序隐式关联,默认情况下,第一个值分配了值0,第二个值分配了1,依此类推。

因此,如果您以这种方式声明了枚举,那么事情就会起作用:
public enum PinSize {
    SMALLEST,  // ordinal value: 0
    SMALL,     // ordinal value: 1
    NORMAL,    // ordinal value: 2
    BIG,       // ordinal value: 3
    BIGGER,    // ordinal value: 4
    BIGGEST;   // ordinal value: 5
}

10-08 00:25