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/elseDart 支持 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/continuewhile/do-while/break/continue 和其他语言没有什么区别, 在这里就不赘述了
switch-caseSwitch 语句可以接受 整型、字符串 、枚举 或者 编译时常量 , 然后使用 == 进行比较, 下面看下常量的比较: 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 的 操作符, 控制流, 异常处理 就介绍完毕 相关推荐函数也可以赋值给变量,存储在数组,切片,映射中,也可以作为参数传递给函数或作为函数返回值进行返回。函数的参数,数量,对应类型,以及函数返回值称为函数的签名。 1函数赋值给变量 示例: 来看这样的示例. 定义了add函数,而后在main中将add赋值给f,而后打印出add和f的类型 package main import "fmt" func add(a,b int)int{ return a + 首先简单介绍下go语言,其主要具有以下基础特点: 1. 简洁、清晰:Go语言的语法非常简洁,易于学习,也易于阅读和维护。 2. 并发支持:Go语言内建对并发编程的支持,可通过Goroutines和Channels实现的。 3. 垃圾回收:Go语言有内置的垃圾回收机制,这使得内存管理变得更加容易。 4. 快速编译:Go语言的编译速度非常快,这使得开发过程更加高效。 5. 标准库:Go语言有一个强大的 A Fast Intro to Git Internals Git内部原理的快速介绍 原文:sites.google.com/a/chromium.… Many git tutorials focus on a set of commands and instructions to “get you up to speed” in git, without addressing the under 在这里,我们将通过使用 C 或 C++ 中的 system() 函数看到一些令人惊奇的结果。该系统功能存在于Windows、Linux和MAC操作系统中。该函数用于执行可以在命令行中编写的系统命令。 这里我们将看到系统函数在C或C++中的两种用法。第一个是使用C++程序获取IP配置详细信息。 示例 #include #include using namespace std; int main() 使用浏览器应用:iPad 上常见的浏览器应用如 Safari、Chrome 等都可以用来打开 HTML 文件。您可以通过电子邮件、云存储服务或者其他方式将 HTML 文件传输到 iPad 上,然后在浏览器中打开该文件。您也可以将 HTML 文件发送到自己的电子邮件,然后在 iPad 上使用邮件应用打开附件。 使用文件管理应用:iPad 上的文件管理应用(如文件或者云存储服务的应用)可以帮助您管理和 回到顶部 |