我有一个简单的类,在正确编译自动装箱Integer时,它坚持要将参数更改为布尔值。我使用的是JDK1.8,否则编译器会抱怨Integer转换。我看不出我做错了什么?所有的包装类都可以自动打开盒子,或者我这么想?
public class MsgLog<Boolean,String> {
private boolean sentOk ;
private Integer id ;
private int id2 ;
public boolean isSentOk() {
return sentOk;
}
public String getTheMsg() {
return theMsg;
}
private String theMsg ;
private MsgLog(Boolean sentOkp, String theMsg)
{
this.sentOk = sentOkp ; // compile error - autoboxing not working
this.theMsg = theMsg ;
this.id = 2; // autoboxing working
this.id2 = (new Integer(7)) ; // autoboxing working the other way around as well
}
}自动装箱不是双向过程吗?
Compile error on jdk 8 (javac 1.8.0_25)
Multiple markers at this line
- Duplicate type parameter String
- The type parameter String is hiding the type String
- The type parameter Boolean is hiding the type
Boolean发布于 2014-05-01 13:54:31
你的问题是第一句:
public class MsgLog<Boolean,String> 您正在声明名为"Boolean“和"String”的类型参数。它们隐藏了实际的Boolean和String类型。据我所见,您甚至不需要该类的类型参数;只需删除它们即可。如果您确实希望保留它们,则应该重命名它们以避免隐藏现有类型。
在语义上,您发布的代码等价于(为了简洁起见,有些代码片段):
public class MsgLog<T,U> {
private boolean sentOk ;
private U theMsg ;
private MsgLog(T sentOkp, U theMsg)
{
this.sentOk = sentOkp ; // compile error - assignment to incompatible type
this.theMsg = theMsg ;
}
}https://stackoverflow.com/questions/23408987
复制相似问题