Vector(x + v.x, y + v.y);
// 重载 - 操作符
Vector operator -(Vector v) => Vector(x - v.x, y - v.y);
// 重载 == 操作符
bool operator ==(other) => other is Vector
&& runtimeType == other.runtimeType
&& x == other.x && y == other.y;
}
void main() {
final v = Vector(2, 3);
final w = Vector(2, 2);
assert(v + w == Vector(4, 5));
assert(v - w == Vector(0, 1));
}
复制代码
如果我们重载了 == 操作符, 也要重载 hashCode 函数, 类似 Java 中重载了 equals 函数,最好也重载 hashCode 函数是一样的, 因为对象的 hash 值决定对象的存储位置
@override
int get hashCode => x.hashCode ^ y.hashCode;
复制代码
控制流语句
if/else
Dart 支持 if 语句和可选的 else 语句
if (isRaining()) {
print("raining");
} else if (isSnowing()) {
print("snowing");
} else {
print("unknown");
}
复制代码
for循环
Dart 不仅支持标准的 for 循环:
for (var i = 0; i < 5; i++) {
}
复制代码
还支持 for-in 那些实现了 Iterator 接口的类(如List/Set):
var collection = [0, 1, 2];
for (var x in collection) {
print(x); // 0 1 2
}
复制代码
通过上一篇文章(三)Flutter学习之Dart函数的介绍知道
Dart Closures 能够访问自身作用域内的变量, 哪怕这个变量是外部传递给 Closure 的 如:
var callbacks = [];
for (var i = 0; i print(i));
}
callbacks.forEach((c) => c());
// 输出
1
2
复制代码
但是在 JavaScript 中会输出两个 2
while/do-while/break/continue
while/do-while/break/continue 和其他语言没有什么区别, 在这里就不赘述了
switch-case
Switch 语句可以接受 整型、字符串 、枚举 或者 编译时常量 , 然后使用 == 进行比较, 下面看下常量的比较:
class Person {
final String id;
const Person(this.id);
}
const p = Person("001");
const p1 = Person("001");
const p2 = Person("003");
switch (p) {
case p1:
print(p1.id);
break;
case p2:
print(p2.id);
break;
default:
print("unknown");
}
复制代码
如果 case 子句不是空, 要以 break/return/throw/continue 结尾, 否则会报错
如果想要 case 子句之间 fall-through 的话, 将 case 子句为空即可
var command = 'CLOSED';
switch (command) {
case 'CLOSED':
case 'NOW_CLOSED':
print("NOW_CLOSED");
break;
default:
print("UNKNOWN");
}
复制代码
还可以使用 continue 的方式 fall-through
var command = 'CLOSED';
switch (command) {
case 'CLOSED':
print("CLOSED");
continue nowClosed;
nowClosed:
case 'NOW_CLOSED':
print("NOW_CLOSED");
break;
default:
print("UNKNOWN");
}
// 输出
CLOSED
NOW_CLOSED
复制代码
断言(assert)
在开发阶段, 我们可以使用断言语句 assert(condition, optionalMessage); 来中断程序的正常执行, 当 condition 为 false 的时候(抛出 AssertionError 异常); 如果 condition 为 true , 则继续执行程序的下一行代码. 例如:
// Make sure the value is less than 100.
assert(number < 100);
// Make sure this is an https URL.
assert(urlString.startsWith('https'));
assert(urlString.startsWith('https'),
'URL ($urlString) should start with "https".');
复制代码
断言什么时候生效呢?这取决于你使用的工具和框架:
- Flutter 在
debug 模式启用断言
- 仅开发阶段使用的开发工具如
dartdevc , 默认是开启断言
- 诸如
dart 、dart2js 等工具支持命令行来启用断言: --enable-asserts
在生产环境的代码, 断言语句将会被忽略, 断言的 condition 表达式不会被执行,所以不用担心性能问题
异常处理
Dart 提供了 throw, rethrow, try, catch, on, finally 关键字让开发者能够抛出和捕获异常
和 Java 不同的是, Dart 中所有的异常都是 unchecked 异常, 也就是说编译器不会强制开发者去捕获任何异常, 除非你有这个需要
Dart 提供了两个类异常: Exception 和 Error , Dart 不仅可以抛出异常还可以抛出任何不为 null 的对象:
// 抛出异常
throw FormatException('Expected at least 1 section');
// 抛出不为 null 的对象
throw 'Out of llamas!';
复制代码
虽然 Dart 允许我们抛出一个不为 null 的普通对象,但是官方还是建议我们抛出的异常继承自 Exception 或 Error
介绍完了 throw 关键字,我们来看下 catch 和 on 和 关键字:
catch 和 on 都是用来捕获异常:
try {
breedMoreLlamas();
} on OutOfLlamasException {
// 捕获一个特定的异常
buyMoreLlamas();
} on Exception catch (e) {
// 捕获所有继承自 Exception 的异常,并拿到异常对象
print('Unknown exception: $e');
} catch (e) {
// 捕获所有异常
print('Something really unknown: $e');
}
复制代码
可见, on 关键字用于指定捕获特定的异常, catch 关键字用于拿到异常对象
catch 关键字除了可以拿到异常对象, 还可以拿到异常的 堆栈 信息, 如:
try {
// ···
} on Exception catch (e) {
print('Exception details:\n $e');
} catch (e, s) {
print('Exception details:\n $e');
print('Stack trace:\n $s');
}
复制代码
一般情况下, 使用了 on, catch 关键字来捕获异常, 异常会停止传播, 如果需要异常继续传播可以使用 rethrow 关键字
void misbehave() {
try {
dynamic foo = true;
print(foo++); // Runtime error
} catch (e) {
print('misbehave() partially handled ${e.runtimeType}.');
rethrow; // Allow callers to see the exception.
}
}
void main() {
try {
misbehave();
} catch (e) {
print('main() finished handling ${e.runtimeType}.');
}
}
复制代码
最后介绍 Dart 异常处理的最后一个关键字 finally
finnaly 关键字很简单 , 就是不管是否抛出异常 finally 子句一定会执行:
try {
breedMoreLlamas();
} finally {
// 就算抛出异常(程序中断执行), finnaly 也会先执行
cleanLlamaStalls();
}
try {
breedMoreLlamas();
} catch (e) {
// 捕获异常
print('Error: $e');
} finally {
// 执行 finally 子句
cleanLlamaStalls(); // Then clean up.
}
复制代码
关于 Dart 的 操作符, 控制流, 异常处理 就介绍完毕
相关推荐
Gitee 是一款国内开源社区和 Git 托管平台,提供了代码托管、团队协作、接口管理等功能,是广大开发者进行开源项目管理的首选平台之一。在使用 Gitee 之前,我们需要注册一个 Gitee 账号,下面就为大家介绍一下如何填写 Gitee 注册信息。 打开 Gitee 官网 首先,我们需要在浏览器中输入 https://gitee.com/ 进入 Gitee 的官方网站。 点击注册 在 Gite
php小编子墨推出新文章“从零到精通:Java JSP 学习捷径”。本文将为读者提供全面、系统的Java JSP学习指南,帮助初学者快速掌握相关知识和技能。无论您是想要入门Java JSP,还是希望提升自己的技能水平,本文都将为您揭示学习的捷径,助您更快地掌握Java JSP编程的精髓。让我们一起开始这段学习之旅,探寻Java JSP的奥秘! 设定学习目标 确定目标:明确你学习 Java JSP
大家好,我是前端西瓜哥。 图形编辑器,随着功能的增加,通常都会愈发复杂,良好的架构是保证图形编辑器持续开发高效的重要技术。 根据功能拆分成一个一个的小模块基本是家常便饭。那么模块之间是如何配合以及进行数据传输的呢? 编辑器 github 地址: https://github.com/F-star/suika 线上体验: https://blog.fstars.wang/app/suika/ 注入
A switch statement can test multiple conditions just like an else clause and handles the default possibility. The default clause can be executed when none of the cases match, and a break statement can
订婚数是两个数字的对,其除数之和等于另一个数字。 例如 (a, b) 是一对订婚数,如果 s(a) = b + 1 且 s(b) = a + 1,其中 s(b) 是 b 的等分和:等效条件是 σ(a) = σ(b) = a + b + 1,其中 σ 表示除数和函数。 前几对订婚号码为:(48, 75)、(140, 195)、(1050, 1925) )、(1575, 1648)、(2024, 22
回到顶部
|