Dart 語言簡易教程(一): http://www.reibang.com/p/8a62b1a2fd75
Dart 語言簡易教程(二): http://www.reibang.com/p/b2153a32dd8b
Dart 語言簡易教程(三): http://www.reibang.com/p/6d2495a0d3d7
Dart 語言簡易教程(四)
操作符
Dart 支持各種類型的操作符悴务,并且其中的一些操作符還能進行重載宛畦。
支持的完整操作如下:
Description | Operator | |
---|---|---|
unary postfix | expr++ expr-- () [] . ?. | |
unary prefix | -expr !expr ~expr ++expr --expr | |
multiplicative | * / % ~/ | |
additive | + - | |
shift | << >> | |
bitwise AND | & | |
mbitwise XOR | ^ | |
bitwise OR | | | |
relational and type test | >= > <= < as is is! | |
equality | == != | |
logical AND | && | |
logical OR | ||
if null | ?? | |
conditional | expr1 ? expr2 : expr3 | |
cascade | .. | |
assignment | = *= /= ~/= %= += -= <<= >>= &= ^= |= ??= |
在上表中凡蜻,操作符的優(yōu)先級依次降低良狈。就是排在最上面的一行優(yōu)先級最高,最后的一行優(yōu)先級越低骤星。
算術(shù)操作符
Dart 支持的基本算術(shù)操作符如下表所示:
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 integer
assert(5 % 2 == 1); // Remainder
print('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
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
關(guān)系操作符
Operator | Meaning |
---|---|
== | Equal; |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Dart 支持的關(guān)系操作符列表:
Operator | Meaning |
---|---|
== | Equal; |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
假如要比對
x
與y
是否相等冒版,==
操作符的工作流程如下:
- 如果
x
或者y
都是null
類型,比較的結(jié)果是true
歌殃。- 如果
x
或者y
只有一個是null
類型乔妈,比較的結(jié)果是false
。- 返回調(diào)用
x.==(y)
的結(jié)果作為==
操作的結(jié)果氓皱。
類型比較操作符
Dart 支持在運行時比較對象的類型路召,支持的操作如下:
Operator | Meaning |
---|---|
as | Typecast |
is | True if the object has the specified type |
is! | False if the object has the specified type |
is
操作,用來比較前操作數(shù)是否是后操作數(shù)的對象波材。
as
操作股淡,用來將前操作數(shù)指定為后操作數(shù)的類型。
指定操作符
=
操作符各聘,將后操作數(shù)的值賦給前操作數(shù)揣非。
??=
操作符,如果前操作數(shù)是null
類型躲因,則將后操作數(shù)賦值給前操作數(shù)早敬;如果前操作數(shù)不等于null
,則保持前操作數(shù)的值發(fā)生變化。
組合指定操作符
Dart 支持將算術(shù)操作符與=
組合起來實現(xiàn)更復(fù)雜的功能大脉。
- | - | - | - | - | -
--- | --- | --- | --- | --- | ---
= | –= | /= | %= | >>= | ^=
+= | *= | ~/= | <<= | &= | =
下表演示了這是如何工作的:
| Compound assignment | Equivalent expression
--- | --- | ---
For an operator op: | a op= b | a = a op b
Example: | a += b | a = a + b
實例:
var a = 2; // Assign using =
a *= 3; // Assign and multiply: a = a * 3
assert(a == 6);
邏輯操作符
Operator | Meaning | |
---|---|---|
!expr | inverts the following expression (changes false to true, and vice versa) | |
logical OR | ||
&& | logical AND |
Dart 支持的邏輯操作符如下表:
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...
}
位操作符及位移操作符
Dart 針對整數(shù)(int 類型)支持位操作符及位移操作符搞监。
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
條件表達式
Dart 支持條件表達式,同時為了減少代碼镰矿,也提供了簡化的操作符琐驴。
Dart中有兩種方式簡化類似其它語言的if-else 功能。
- 使用
? :
表達式。
使用如下的表達式:
condition ? expr1 : expr2
condition
的值為true, 則返回結(jié)果為expr1
绝淡;反之則返回expr2
宙刘。
- 使用
??
表達式。
使用如下的表達式:
expr1 ?? expr2
如果expr1
的值為non-null牢酵,則返回結(jié)果為expr1
悬包;反之則返回expr2
。
下面的例子演示了:將Dart 的? :
操作符轉(zhuǎn)化為'if-else' 的版本:
// Slightly longer version uses ?: operator.
String toString() => msg == null ? super.toString() : msg;
// Very long version uses if-else statement.
String toString() {
if (msg == null) {
return super.toString();
} else {
return msg;
}
}
級聯(lián)操作符(..)
通過級聯(lián)操作符(..)馍乙,可以連續(xù)的操作同一對象布近,達到減少中間變量,減少代碼的目的丝格。
如下面的例子:
querySelector('#button') // Get an object.
..text = 'Confirm' // Use its members.
..classes.add('important')
..onClick.listen((e) => window.alert('Confirmed!'));
下面的代碼與上面的例子實現(xiàn)功能完全相同:
var button = querySelector('#button');
button.text = 'Confirm';
button.classes.add('important');
button.onClick.listen((e) => window.alert('Confirmed!'));
另一段例子:
final addressBook = (new AddressBookBuilder()
..name = 'jenny'
..email = 'jenny@example.com'
..phone = (new PhoneNumberBuilder()
..number = '415-555-0100'
..label = 'home')
.build())
.build();
其它的一些操作符
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) |