繁体   English   中英

将返回方法从布尔值更改为整数

[英]Change return method from boolean to int

我可以将这种方法从boolean转换为int吗? 因此,当我将调用该方法而不是返回true或false时,我可以返回1或0:

public boolean openMode() {

return Settings.System.getBoolean(contxt.getConentResolver(), Setting.System.START_METHOD, true); 

}
public int openMode(){
    boolean value = Settings.System.getBoolean(contxt.getConentResolver(), Setting.System.START_METHOD, true);

    if(value){
        return 1;
    }
    else{
        return 0;
    }

}

与其他编程语言(如C)不同,java不会将1或0识别为true或false(或简称为boolean)。 因此,此方法的返回类型必须是self布尔值,除非更改方法的返回类型,否则不能返回1或0。 但是,如果可以更改方法签名,则可以将返回类型更改为int,然后将1表示为true,将0表示为false。 例如:

public int openMode(){
 return (Settings.System.getBoolean(contxt.getConentResolver(), Setting.System.START_METHOD, true))?1:0 ;
}

较短的形式,使用“三元运算符”:

public int openMode()
{
    boolean value = Settings.System.getBoolean(contxt.getConentResolver(), Setting.System.START_METHOD, true);
    return (value == true ? 1 : 0);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM