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. 查找并修复Android中的内存泄露—OutOfMemoryError

    [编者按]本文作者为来自南非约翰内斯堡的女程序员 Rebecca Franks,Rebecca 热衷于安卓开发,拥有4年安卓应用开发经验.有点完美主义者,喜爱美食. 本文系国内ITOM管理平台 One ...

  2. url用法

    url中的name用法: 0.定义主rul.py urlpatterns = [ url(r'^sinfors/', include('sinfors.urls', namespace="s ...

  3. Celery学习--- Celery在项目中的使用

    可以把celery配置成一个应用,注意连接文件命名必须为celery.py 目录格式如下 项目前提: 安装并启动Redis CeleryPro/celery.py   [命名必须为celery.py] ...

  4. 铁乐学python_Day40_进程池

    进程之间的数据共享 基于消息传递的并发编程是大势所趋, 即便是使用线程,推荐做法也是将程序设计为大量独立的线程集合,通过消息队列交换数据. 这样极大地减少了对使用锁和其他同步手段的需求,还可以扩展到分 ...

  5. Ardunio控制RGB的LED灯显示彩虹渐变色.

    由于我使用的是共阴极的RGB LED,如果你的是共阳极的,接线的时候要注意一下. 其他没什么不同 //定义RGB色彩的输出I/O ; ; ; //标记颜色变化的方式,增加值还是减小值 bool red ...

  6. ASP.NET MVC 3 Performance – on par with MVC 2

    http://blogs.msdn.com/b/marcinon/archive/2011/01/17/mvc-3-performance.aspx ASP.NET MVC 3 Performance ...

  7. 【模块化】 RequireJS入门教程总结与推荐

    之所以学习RequireJS,肯定对 模块化有一定的理解.这里有几篇学习 RequireJS的文章,推荐给大家去学习. Javascript模块化编程(一):模块的写法 Javascript模块化编程 ...

  8. 【笔记】关于TCP三次握手和四次挥手的理解

    1. 三次握手: 服务器一定处于Listen状态,否则客户端发过来的连接会被拒绝.注:服务器和客户端的角色是相对的. 客户端发送第一次握手(客户端发送连接请求(SYNC包)到服务器)之后由Closed ...

  9. bootstrap-multiselect样式修改

    问题 bootstrap-multiselect是一款相当不错的bootstrap风格下拉框组件,但是它的某些样式我不是很喜欢,按钮文本和下拉符号 “” 都是居中的,且下拉列表的宽度也没有跟随变动. ...

  10. Android Studio3.0 配置AndroidAnnotation注解框架

    前言android学习了一段时间后,想要开发一款App,但是一些复杂的代码写多了实在麻烦,就到网上找了找简便的方法,于是在众多的注解开发框架中,找到了Android Annotation这个框架,这里 ...