将实现相同接口的枚举添加到集合中会抛出 ClassCastException

Adding enums, implementing same interface, to a collection throws ClassCastException

我制作了一个接口 Attribute 由不同的枚举实现(据我所知 Java-枚举实际上是 class)代表一个属性:

接口Attribute:

public interface Attribute {
    void print();
}

然后我做了两个枚举:SizeType 以及 Attribute 行为:

枚举Size:

public enum Size implements Attribute {
    SMALL, MEDIUM, LARGE;

    @Override
    public void print() {
        System.out.println("I am a size.");
    }
}

枚举Type:

public enum Type implements Attribute {
    SQUARE, CIRCLE, TRIANGLE;

    @Override
    public void print() {
        System.out.println("I am a type.");
    }
}

然后我测试我的主结构 class:

public class AttributesTestDrive {
    public static void main(String[] args) {
        Set<Attribute> attributes = new TreeSet<>();
        attributes.add(Size.MEDIUM);
        attributes.add(Type.CIRCLE);
        for (Attribute attribute : attributes) {
            attribute.print();
        }
    }
}

任何人都可以解释为什么我得到 java.lang.ClassCastException 以及如何摆脱它:

异常详情:

at java.lang.Enum.compareTo(Enum.java:180)
at java.lang.Enum.compareTo(Enum.java:55)
at java.util.TreeMap.put(TreeMap.java:568)
at java.util.TreeSet.add(TreeSet.java:255)

尝试使用 HashSet 而不是 TreeSet