我仍然是 Dart 的新手,但具有以前的知识(和阅读 dart 语言之旅):
condition ? expr1 : expr2其值为expr1or expr2。一个statement 包含 expressions,但一个expression 不能包含一个statement。
以上是我对我试图为您简化的要点的解释,在阅读有关类别重要概念的语言之旅时发现,如下所示:
Dart 有表达式(有运行时值)和语句(没有)。例如,条件表达式
condition ? expr1 : expr2的值为expr1orexpr2。将其与没有价值的if-else 语句进行比较。一个语句通常包含一个或多个表达式,但一个表达式不能直接包含一个语句。
表达式是值,语句做事。
如果你能看到例子,这更有意义。
表达式在运行时具有值。
42truehello1 + 1xmyObjectmyInt + 1k++p > 0condition ? expr1 : expr2'hello'.toUpperCase()myObject.toString()myObject.someMethod()myObject?.someProperty?.someMethod()myString.isEmpty[1, 2, 3][...list1]<String, String>{...a, ...b}一个语句会做一些事情,它本身在运行时没有价值。语句不是表达式,但它们可以包含表达式。
myInt = 1;print('hello');return null;if (name != null) { return name; } else { return 'Guest'; }for (var i = 0; i < 5; i++) { message.write('!'); }break;while (!isDone()) { doSomething(); }yield k++;assert(text != null);throw FormatException('Expected at least 1 section');void distanceTo(Point other) => throw UnimplementedError();注意:此处的大多数示例都是通过在文档中搜索关键字expression和statement 获得的。
| 归档时间: |
|
| 查看次数: |
1404 次 |
| 最近记录: |