为什么post增量运算符不能处理返回int的方法? [英] Why doesn't the post increment operator work on a method that returns an int?

查看:138
本文介绍了为什么post增量运算符不能处理返回int的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void increment(){
    int zero = 0;

    int oneA = zero++; // Compiles

    int oneB = 0++; // Doesn't compile

    int oneC = getInt()++; // Doesn't compile
}

private int getInt(){
    return 0;
}

它们都是int,为什么B& C编译?与 ++ 运算符与 = 0 + 1;

They are all int's, why won't B & C compile? Is it to do with the way ++ operator differs from = 0 + 1;?


操作的参数无效++ / -

Invalid argument to operation ++/--


推荐答案

i ++ 是变量的赋值 i

在您的情况下,零++ 相当于零=零+ 1 。所以 0 ++ 意味着 0 = 0 + 1 ,这没有任何意义,以及 getInt()= getInt()+ 1

In your case, zero++ is an equivalent to zero = zero + 1. So 0++ would mean 0 = 0 + 1, which makes no sense, as well as getInt() = getInt() + 1.

更准确:

int oneA = zero++;

表示

int oneA = zero;
zero = zero + 1; // OK, oneA == 0, zero == 1







int oneB = 0++;

表示

int oneB = 0;
0 = 0 + 1; // wrong, can't assign value to a value.







int oneC = getInt()++;

表示

int oneC = getInt();
getInt() = getInt() + 1; // wrong, can't assign value to a method return value.






从更一般的角度来看,变量是 L值,这意味着它指的是内存位置,因此可以分配。 L - 值中的 L 代表赋值运算符的侧(即 = ),即使可以在赋值运算符的左侧或右侧找到L值(例如 x = y )。


From a more general point of view, a variable is a L-value, meaning that it refers to a memory location, and can therefore be assigned. L in L-value stands for left side of the assignment operator (i.e. =), even if L-values can be found either on the left side or the right side of the assignment operator (x = y for instance).

相反的是 R值 R 代表赋值运算符的侧)。 R值只能在赋值语句的右侧使用,以便为L值赋值。通常,R值是文字(数字,字符串......)和方法。

The opposite is R-value (R stands for right side of the assignment operator). R-values can be used only on the right side of assignment statements, to assign something to a L-value. Typically, R-values are literals (numbers, characters strings...) and methods.

这篇关于为什么post增量运算符不能处理返回int的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆