Java Android-从另一个类调用布尔值
如果我在一个类中将值设置为true,例如Java Android-从另一个类调用布尔值,java,android,boolean,return-value,Java,Android,Boolean,Return Value,如果我在一个类中将值设置为true,例如 static boolean mIsPremium = true; 有时我只能得到正确的值,因为如果我使用双等号(=),我将始终返回真实值 if (firstClass.mIsPremium == true){ do stuff } else { do other stuff } 如果我只使用一个等号(=),它将返回一个真值,当我在第一个类中将mIsPremium设置为false时就会发生这种情况 if (firstClass.mIs
static boolean mIsPremium = true;
有时我只能得到正确的值,因为如果我使用双等号(=),我将始终返回真实值
if (firstClass.mIsPremium == true){
do stuff
} else {
do other stuff
}
如果我只使用一个等号(=),它将返回一个真值,当我在第一个类中将mIsPremium设置为false时就会发生这种情况
if (firstClass.mIsPremium = true){
do stuff
} else {
do other stuff
}
我尝试了很多配置,使用了两个=符号,使用了一个=符号,包括第二个类中的一个新布尔值,它的值取决于第一个布尔值。。。我所做的一切似乎都不正常
如何从另一个类调用布尔值并在第二个类中正确使用该值。如果它是真的,我希望它在第二节课上是真的,如果它是假的,我希望它在第二节课上是假的。你不需要使用任何=符号,你可以这样做
if(firstClass.mIsPremium) { } // check for premium
或
你对这个问题感到困惑
是指定值
==
检查是否相等
if (firstClass.mIsPremium = true){
上行将其视为一条语句,并将值true赋值给mIsPremium,然后继续
if (firstClass.mIsPremium == true){
检查两个操作数的值是否相等,如果是,则条件变为真
你所做的是
if (firstClass.mIsPremium){
do stuff
} else {
do other stuff
}
One=sing是一个赋值,赋值总是true
(a = b) #-> always true, it doesn't matter if a or b are false or true
Two=sing是一个比较,结果取决于a和b的值
(a == b) #-> true if and only if a has the same value of b
布尔值只能有两个值,即true/false。无需使用==或= 只用
if (firstClass.mIsPremium) //if mIsPremium is true
{
do stuff
} else {
do other stuff
}
我改为“如果(头等舱,错配){…'并且我没有得到正确的返回值。原始值是'static boolean mIsPremium=true;'我希望在if语句下弹出一条toast消息,但什么也没有发生。为什么它不返回值为true并显示toast呢?另外,if语句发生在onCreate中,如果这有区别的话。
(a == b) #-> true if and only if a has the same value of b
if (firstClass.mIsPremium) //if mIsPremium is true
{
do stuff
} else {
do other stuff
}