一个三元运算符内的"+="运算符代表什么?

huangapple go评论68阅读模式
英文:

What does a "+=" operator inside a ternary operator means?

问题

我在androidx.lifecycle包中找到了一段代码片段,我想知道这是什么意思。

LiveData.this.mActiveCount += mActive ? 1 : -1;

其中mActiveCount是一个整数,mActive是一个布尔值。

但是,在我写这个问题的时候,我想我已经想出了答案,所以如果我没弄错的话,"+= "运算符的作用就像我们通常使用的"= "运算符一样。

这意味着代码执行的顺序如下:

首先执行mActive ? 1 : -1;这部分。

一旦这个表达式得到解析,接着执行LiveData.this.mActiveCount += mActive。所以我的真正问题是:

这段代码的等价写法是否正确?:

int intToAdd = mActive ? 1 : -1;
activeCount += intToAdd;

英文:

I came across a code snippet inside the androidx.lifecycle package and I was wondering what does this means.

LiveData.this.mActiveCount += mActive ? 1 : -1;

Where mActiveCount is an int, and mActive is a boolean.

But, as I was writting this question, I think I came with the answer, so if I'm not mistaken the "+=" operator, is used as we normally use the "=" operator.

This means that the order in which the code executes is the following:

the mActive ? 1 : -1; portion executes first.

Once this is resolved, the LiveData.this.mActiveCount += mActive executes. So my real question is:

Is this the correct equivalence of this code?:

    int intToAdd = mActive ? 1 : -1;
    activeCount += intToAdd;

答案1

得分: 9

操作符+=与三元运算符无关。
您正在使用三元运算符检查条件,并通过增加或减少1来递增或递减变量。

假设我们之前已经声明了变量a和b,那么a = a + b等同于a += b

因此,您的代码LiveData.this.mActiveCount += mActive ? 1 : -1;等同于:

if(mActive){
    LiveData.this.mActiveCount += 1;
}
else{
    LiveData.this.mActiveCount -= 1;
}

您下面的逻辑也是正确的:

int intToAdd = mActive ? 1 : -1;
activeCount += intToAdd;
英文:

The operator += is not concerned with ternary operator.
You are checking for a condition using ternary operator and incrementing or decrementing it variable by 1.

a = a + b is equivalent to a += b, assuming we have declared a and b previously.

So, your code LiveData.this.mActiveCount += mActive ? 1 : -1; is equivalent to :-

 if(mActive){
    LiveData.this.mActiveCount += 1;
 }
 else{
   LiveData.this.mActiveCount -= 1;
 }

Your Logic below is also correct:-

 int intToAdd = mActive ? 1 : -1;
 activeCount += intToAdd;

答案2

得分: 3

这行代码会将1或-1添加到 mAtiveCount,并根据布尔值 mActive 来决定是添加+1还是-1。

这与下面的代码片段完全等效,我删除了三元操作符和+=操作符的使用(并明确了它们的功能):

int amountToAdd;
if (mActive) {
  amountToAdd = 1;
} else { 
  amountToAdd = -1;
}
LiveData.this.mActiveCount = LiveData.this.mActiveCount + amountToAdd;

我认为这行代码有点不太清晰,但通过巧妙使用括号可以使其更清晰:

LiveData.this.mActiveCount += (mActive ? 1 : -1);
英文:

This line of code adds either 1 or -1 to mAtiveCount, and looks at the boolean mActive to determine whether it adds +1 or -1.

It is exactly equivalent to this chunk of code, where I removed the usage of the tertiary operator and the += operator (and made their function explicit):

int amountToAdd;
if (mActive) {
  amountToAdd = 1;
} else { 
  amountToAdd = -1;
}
LiveData.this.mActiveCount = LiveData.this.mActiveCount + amountToAdd;

I think the line is a bit unclear, but could be made more clear with the judicious use of parenthesis:

LiveData.this.mActiveCount += (mActive ? 1 : -1);

答案3

得分: 2

是的,你说得对。在Java中有一种叫做“简写”的东西。

例如:

sum = sum + 1 可以写成 sum += 1

这个语句:

LiveData.this.mActiveCount += mActive ? 1 : -1;

这个语句的实际意思是:

根据 mActive 的值(true或false),要么执行 LiveData.this.mActiveCount += 1,要么执行 LiveData.this.mActiveCount += -1

英文:

Yes, you are right. There is something called as shorthand in java .

For example :

sum = sum + 1 can be written as sum += 1.

This statement :

LiveData.this.mActiveCount += mActive ? 1 : -1;

This statement really mean to say :

Either do this LiveData.this.mActiveCount += 1 or LiveData.this.mActiveCount += -1 based on mActive's value (true or false)

答案4

得分: 2

这可以通过查找Java运算符优先级来回答。

赋值运算符的优先级最低,其他所有操作在此之前发生。条件表达式 mActive ? 1 : -1 首先被计算。然后使用条件表达式的结果来计算 += 运算符。

英文:

This can be answered by looking up Java operator precedence.

Assignment operators have the absolute lowest precedence, everything else happens first. The conditional expression mActive ? 1 : -1 is evaluated first. then the += is evaluated using the result of the condition expression.

huangapple
  • 本文由 发表于 2020年9月24日 03:09:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64034772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定