javac error: inconvertible types with generics?

后端 未结 4 1917
醉梦人生
醉梦人生 2020-12-24 02:33

There are several other SO questions talking about generics compiling OK w/ Eclipse\'s compiler but not javac (i.e. Java: Generics handled differenlty in Eclipse and javac a

4条回答
  •  臣服心动
    2020-12-24 02:53

    Perhaps it is because you've declared E as something that extends Enum. I can't say I understand it completely, but it looks like it limits the set of types to some subset that can't include LogEvent.Type for some reason. Or maybe it's just a bug in the compiler. I'd be happy if someone could explain it more clearly, but here is what you can do:

    public > void postEvent(E code) 
    {
        if (code instanceof LogEvent.Type)
        {
            LogEvent.Type scode = (LogEvent.Type)code;
            ...
        }
        ...
    

    This works and it is more elegant than just casting to an Object.

提交回复
热议问题