首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动装箱不适用布尔值

自动装箱不适用布尔值
EN

Stack Overflow用户
提问于 2014-05-01 13:46:44
回答 1查看 683关注 0票数 0

我有一个简单的类,在正确编译自动装箱Integer时,它坚持要将参数更改为布尔值。我使用的是JDK1.8,否则编译器会抱怨Integer转换。我看不出我做错了什么?所有的包装类都可以自动打开盒子,或者我这么想?

代码语言:javascript
复制
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

    }

}

自动装箱不是双向过程吗?

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-01 13:54:31

你的问题是第一句:

代码语言:javascript
复制
public class MsgLog<Boolean,String> 

您正在声明名为"Boolean“和"String”的类型参数。它们隐藏了实际的BooleanString类型。据我所见,您甚至不需要该类的类型参数;只需删除它们即可。如果您确实希望保留它们,则应该重命名它们以避免隐藏现有类型。

在语义上,您发布的代码等价于(为了简洁起见,有些代码片段):

代码语言:javascript
复制
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 ;
    }

}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23408987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档