码迷,mamicode.com
首页 > 其他好文 > 详细

4.Operators-操作符(Dart中文文档)

时间:2018-12-21 00:56:40      阅读:705      评论:0      收藏:0      [点我收藏+]

标签:常见   following   rip   des   The   一个   mod   val   ada   

Dart有如下操作符:

Description Operator
unary postfix expr++ expr-- () [] . ?.
unary prefix -expr !expr ~expr ++expr --expr
multiplicative * / % ~/
additive + -
shift << >>
bitwise AND &
bitwise XOR ^
bitwise OR |
relational and type test >= > <= < as is is!
equality == !=
logical AND &&
logical OR ||
if null ??
conditional expr1 ? expr2 : expr3
cascade ..
assignment = *= /= ~/= %= += -= <<= >>= &= ^= |= ??=

在操作符表中,优先级是从上到下递减。例如,即便&&在==之前,优先级是求余(%)>判断相等(==)> 且(&&)。下面代码中,计算顺序是一致的

// Parentheses improve readability.
if ((n % i == 0) && (d % i == 0)) ...

// Harder to read, but equivalent.
if (n % i == 0 && d % i == 0) ...

注意:操作符左右各有一个对象,操作符具体行为是由左边的对象决定的,这个涉及到操作符重载,比如Vector和Point对象重载了+号操作符的行为,Vector+Point的+号具体行为由Vector决定。

Arithmetic operators 算术运算符

Dart supports the usual arithmetic operators, as shown in the following table.
Dart支持常见的几种算术运算符

Operator Meaning
+ Add
Subtract
-expr Unary minus, also known as negation (reverse the sign of the expression)
* Multiply
/ Divide
~/ Divide, returning an integer result
% Get the remainder of an integer division (modulo)
assert(2 + 3 == 5);
assert(2 - 3 == -1);
assert(2 * 3 == 6);
assert(5 / 2 == 2.5); // Result is a double
assert(5 ~/ 2 == 2); // Result is an int
assert(5 % 2 == 1); // Remainder

assert(‘5/2 = ${5 ~/ 2} r ${5 % 2}‘ == ‘5/2 = 2 r 1‘);

Dart也支持自增自减运算符

Operator Meaning
++var var = var + 1 (expression value is var + 1)
var++ var = var + 1 (expression value is var)
--var var = var – 1 (expression value is var – 1)
var-- var = var – 1 (expression value is var)
var a, b;

a = 0;
b = ++a; // Increment a before b gets its value.
assert(a == b); // 1 == 1

Equality and relational operators 关系运算符

Operator Meaning
== Equal; see discussion below
!= Not equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

验证两个对象是否相同,用==操作符,(在极少数情况下,你要判断两个对象是否完全相同,需要用identical()函数)
下面是==的判断逻辑:
步骤1.如果x,y都是null,返回true ,x,y只有一个是null,返回false
步骤2.返回x.==(y)的结果,这种写法类似a.method(params),可以将==看做x的方法。

assert(2 == 2);
assert(2 != 3);
assert(3 > 2);
assert(2 < 3);
assert(3 >= 3);
assert(2 <= 3);
a = 0;
b = a++; // Increment a AFTER b gets its value.
assert(a != b); // 1 != 0

a = 0;
b = --a; // Decrement a before b gets its value.
assert(a == b); // -1 == -1

a = 0;
b = a--; // Decrement a AFTER b gets its value.
assert(a != b); // -1 != 0
```
本章节未完

4.Operators-操作符(Dart中文文档)

标签:常见   following   rip   des   The   一个   mod   val   ada   

原文地址:https://www.cnblogs.com/lowezheng/p/10146451.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!