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

Type test operators 类操作符

Operator Meaning
as Typecast (also used to specify library prefixes)
is True if the object has the specified type
is! False if the object has the specified type

如果对象A是实现T这个类的,那么 A is T == true,is!正好相反。as的用法是将对象A强制转换为指定类型。

下面的例子是判断对象类型,如果是true,可执行对应类型的方法

if (emp is Person) {
// Type check
emp.firstName = 'Bob';
}

这是上面的简化写法,如果emp是null或者不是Person类型,后续代码不会执行

(emp as Person).firstName = 'Bob';

Assignment operators 赋值运算符

As you’ve already seen, you can assign values using the = operator. To assign only if the assigned-to variable is null, use the ??= operator.

如你所知,可以用=进行赋值,如果只对空对象赋值,可以用??=。

// Assign value to a
a = value;
// Assign value to b if b is null; otherwise, b stays the same
b ??= value;

组合赋值操作符:

= |–= |/= |%= |>>= |^=

+= |*= |~/= |<<= |&=| ||=

等效说明:

Compound assignment Equivalent expression
For an operator op: a op= b a = a op b
Example: a += b a = a + b

The following example uses assignment and compound assignment operators:

var a = 2; // Assign using =
a *= 3; // Assign and multiply: a = a * 3
assert(a == 6);
``` ### Logical operators 逻辑运算符 |Operator| Meaning|
|--|--|
!expr |inverts the following expression (changes false to true, and vice versa)
|| |logical OR
&& |logical AND 下面是逻辑运算符示例:
··· if (!done && (col == 0 || col == 3)) {
// ...Do something...
} ··· ### Bitwise and shift operators 位和移位操作符 你可以对数值进行移位操作 Operator| Meaning
--|--
|&| AND
\|| OR
^| XOR
~expr| Unary bitwise complement (0s become 1s; 1s become 0s)
<<| Shift left
>>| Shift right 下面是移位操作示例:
```
final value = 0x22;
final bitmask = 0x0f; assert((value & bitmask) == 0x02); // AND
assert((value & ~bitmask) == 0x20); // AND NOT
assert((value | bitmask) == 0x2f); // OR
assert((value ^ bitmask) == 0x2d); // XOR
assert((value << 4) == 0x220); // Shift left
assert((value >> 4) == 0x02); // Shift right
```
### Conditional expressions 条件表达式 Dart有两种条件表达式写法,来取代if逻辑。 ```
condition ? expr1 : expr2
```
如果condition是true,返回expr1,否则返回expr2
```
expr1 ?? expr2
```
如果expr1不是null,返回expr1,否则返回expr2.
```
var visibility = isPublic ? 'public' : 'private'; String playerName(String name) => name ?? 'Guest';
``` ```
// Slightly longer version uses ?: operator.
String playerName(String name) => name != null ? name : 'Guest';
// Very long version uses if-else statement.
String playerName(String name) {
if (name != null) {
return name;
} else {
return 'Guest';
}
}
``` ### Cascade notation (..) 链式调用操作符
链式操作符允许你对相同对象进行多次调用而不用多次写对象名。 ```
querySelector('#confirm') // Get an object.
..text = 'Confirm' // Use its members.
..classes.add('important')
..onClick.listen((e) => window.alert('Confirmed!'));
``` 上面的示例等效于:
```
var button = querySelector('#confirm');
button.text = 'Confirm';
button.classes.add('important');
button.onClick.listen((e) => window.alert('Confirmed!'));
```
You can also nest your cascades. For example:
```
final addressBook = (AddressBookBuilder()
..name = 'jenny'
..email = 'jenny@example.com'
..phone = (PhoneNumberBuilder()
..number = '415-555-0100'
..label = 'home')
.build())
.build();
```
注意,链式调用必须由对象发起,比如下面的write返回值为void,所以无法使用链式调用。
```
var sb = StringBuffer();
sb.write('foo')
..write('bar'); // Error: method 'write' isn't defined for 'void'.
``` ### Other operators 其它操作符 Operator| Name| Meaning
--|--|--
() |Function application| Represents a function call
[] |List access |Refers to the value at the specified index in the list
. |Member access |Refers to a property of an expression; example: foo.bar selects property bar from expression foo
?. |Conditional |member access Like ., but the leftmost operand can be null; example: foo?.bar selects property bar from expression foo unless foo is null (in which case the value of foo?.bar is null) 第四篇准备翻译 Control flow statements 流程控制

4.Operators-操作符(Dart中文文档)的更多相关文章

  1. 3.Functions-函数(Dart中文文档)

    初次翻译,部分内容并非按字面翻译,是按本人理解进行了内容重组.如有错误望指正. Dart是完全的面向对象的语言,甚至函数也是一个Function类型的对象.这意味着函数可以赋值给变量或者作为函数的参数 ...

  2. 2.Built-in types-基本数据类型(Dart中文文档)

    初次翻译,部分内容并非按字面翻译,是按本人理解进行了内容重组.如有错误望指正. Dart语言内置如下数据类型: numbers strings booleans lists (所谓的数组) maps ...

  3. 1.Variables-变量(Dart中文文档)

    初次翻译,部分内容并非按字面翻译,是按本人理解进行了内容重组.如有错误望指正. 如下是变量定义和赋值的示例 var name = 'Bob'; 变量存储的是一个引用地址.如上的变量name指向了一个值 ...

  4. 8.Generics 泛型(Dart中文文档)

    这篇翻译的不好 如果你看API文档中的数组篇,你会发现类型一般写成List.<...>的写法表示通用类型的数组(未明确指定数组中的数据类型).通常情况泛型类型用E,T,S,K,V表示. W ...

  5. 7.Classes-类(Dart中文文档)

    Dart是一个面向对象的语言,同时增加了混入(mixin)继承的特性.对象都是由类初始化生成的,所有的类都由Object对象继承.混入继承意味着尽管所有类(除了Object类)只有一个父类,但是类的代 ...

  6. 6.Exceptions-异常(Dart中文文档)

    异常是用于标识程序发生未知异常.如果异常没有被捕获,If the exception isn't caught, the isolate that raised the exception is su ...

  7. 5.Control flow statements-流程控制(Dart中文文档)

    你可以使用如下流程控制符: if and else for loops while and do-while loops break and continue switch and case asse ...

  8. Flutter 中文文档网站 flutter.cn 正式发布!

    在通常的对 Flutter 介绍中,最耳熟能详的是下面四个特点: 精美 (Beautiful):充分的赋予和发挥设计师的创造力和想象力,让你真正掌控屏幕上的每一个像素. ** 极速 (Fast)**: ...

  9. Reactor3 中文文档(用户手册)

    文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...

随机推荐

  1. windows 程序员电脑设置

    程序员电脑设置: 1.详细目录 a.在一个文件夹下设为详细信息 b.win7点击"组织"-->"文件夹的搜索选项"-->"查看" ...

  2. 带你从零学ReactNative开发跨平台App开发(七)

    ReactNative跨平台开发系列教程: 带你从零学ReactNative开发跨平台App开发(一) 带你从零学ReactNative开发跨平台App开发(二) 带你从零学ReactNative开发 ...

  3. node和iisnode express手工安装

    一.安装node.js的x86版本: 这样,node.js会安装在C:\Program Files (x86)\nodejs,这符合iisnode express7版本的期待. 二.安装iisnode ...

  4. JVM参数简述

    java虚拟机启动时会带有很多的启动参数,Java命令本身就是一个多参数的启动命令.那么具体JVM启动包含哪些参数呢?这篇文章针对java8的情况做一篇汇总解读,包含大多数常见和不常见的命令参数,过于 ...

  5. Oracle EBS PO采购订单更新

    DECLARE l_result NUMBER; l_progress NUMBER; l_errors PO_API_ERRORS_REC_TYPE; l_chg PO_CHANGES_REC_TY ...

  6. [翻译] UCZProgressView

    UCZProgressView UCZProgressView is a circular progress indicator with cool animations for image load ...

  7. 局域网不同网段访问设置WINS域名服务系统

    大背景 公司两台路由器,网段不同 路由器:192.168.0.1 路由器:192.168.1.1 路由器2需要访问路由器1的机子,初始是ping不通的. 方案 使用IP设置里WINS设置,即可轻松实现 ...

  8. K8S Deployment 命令

    创建 Deployment kubectl create -f https://kubernetes.io/docs/user-guide/nginx-deployment.yaml --record ...

  9. Hadoop HBase概念学习系列之HBase里的长表VS宽表VS窄表(十五)

    有时候啊,HBase表的设计方案通常,还会考虑如下一些因素,当然,这只是考虑范围里的部分呢. 更多的行还是更多的版本?后者使用了HBase自带的功能.但是需要在列簇中定义最大版本数,这样做可能有风险. ...

  10. JavaScript基础进阶之数组方法总结

    数组常用方法总结:  下面我只总结了es3中常用的数组方法,一共有11个.es5中新增的9个数组方法,后续再单独总结. 1个连接数组的方法:concat() 2个数组转换为字符串的方法:join(). ...