[英]Change return method from boolean to int
Can i convert in some way this method from boolean to int? 我可以将这种方法从boolean转换为int吗? so when i will call the method instead return true or false i can return 1 or 0:
因此,当我将调用该方法而不是返回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;
}
}
Unlike other programming languages like C, java does not recognizes 1 or 0 as true or false ( or boolean in short). 与其他编程语言(如C)不同,java不会将1或0识别为true或false(或简称为boolean)。 So the return type of this method has to be boolean it self, you can not return 1 or 0 unless you change the method's return type.
因此,此方法的返回类型必须是self布尔值,除非更改方法的返回类型,否则不能返回1或0。 However, if you can change the method signature , you can change the return type to int and return 1 for true and 0 for false.
但是,如果可以更改方法签名,则可以将返回类型更改为int,然后将1表示为true,将0表示为false。 example :
例如:
public int openMode(){
return (Settings.System.getBoolean(contxt.getConentResolver(), Setting.System.START_METHOD, true))?1:0 ;
}
A shorter form, using a "ternary operator": 较短的形式,使用“三元运算符”:
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.