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. 一种特殊场景下的HASH JOIN的优化为NEST LOOP.

    应用场景: 有如下的SQL: select t.*, t1.ownerfrom t, t1where t.id=t1.id; 表t ,t1的数据量比较大,比如200W行.但是两张表能关联的行数却很少, ...

  2. 前端HTML空格与后台PHP utf-8空格

    今天在处理html input输入框时,发现一个问题: 在用户名输入框中输入admin "'p(中间是一个空格),点保存后台提示数据保存成功,按理应该是未修改,通过chrome调试工具发现传 ...

  3. leetCode题解 寻找运动环

    1.题目描述 Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this ...

  4. SASS 入门

    为什么使用Sass 作为前端(html.javascript.css)的三大马车之一的css,一直以静态语言存在,HTML5火遍大江南北了.javascript由于NODE.JS而成为目前前后端统一开 ...

  5. [翻译] UIImageView-Letters

    UIImageView-Letters https://github.com/bachonk/UIImageView-Letters An easy, helpful UIImageView cate ...

  6. [翻译] RSKImageCropper

    RSKImageCropper https://github.com/ruslanskorb/RSKImageCropper An image cropper for iOS like in the ...

  7. 多线程应用-类(thread)

    在对class thread加锁时,锁无法正常应用,函数方式没问题. 在使用class thread方法时,并发后的查询结果不对,函数方式没问题. # -*- coding: UTF-8 -*- fr ...

  8. php5 Array 数组函数

    函数 描述 array() 创建数组. array_change_key_case() 把数组中所有键更改为小写或大写. array_chunk() 把一个数组分割为新的数组块. array_colu ...

  9. 企业大数据之Elasticsearch的搜索类型

    下面的 ES基于版本(V2.3.4) ES之默认 1.默认自动发先同一局域网的所有集群节点 2.默认一个索引库会有5个分片,(分片越多,效率越好) 由于这两个默认,所以统一索引库的分片对分布在不同机器 ...

  10. php解析xml文件的方法

    最近一段时间在做模板包导入.模板包中包含有xml文件,,需要解析成给定的php数组格式. 我接触到了两种方法,分别是DOMDocument 方法和 simple_load_file. 个人偏好后一种, ...