dart - 在 Dart 中使用 is-operator 和使用运行时类型有什么区别
问题描述
我已经通过dart codelab 进行了迭代,并偶然发现了这个代码片段:
class EmailAddress {
String address;
EmailAddress(this.address);
@override
bool operator ==(Object other) =>
identical(this, other) ||
(a) other is EmailAddress &&
(b) runtimeType == other.runtimeType &&
address == other.address;
....
(a) 行和 (b) 行有什么区别?对我来说,他们似乎也在做同样的事情。或者更一般地问:使用 is-operator 和使用对象的 runtimeType 属性来检查 dart 中的运行时类型有什么区别?
解决方案
aSet is Iterable
- 这是true
aSet.runtimeType == Iterable
这是false
所以是检查处理子类。
此外,我们真的建议您避免使用runtimeType
. 特别是在编译为 JavaScript 时。它真的会炸毁你编译的应用程序的大小。
我将在那个 codelab 上打开一个问题!
推荐阅读
- java - 使用 onAction 属性返回按钮文本
- linux - 使用英特尔编译器在 linux 机器上配置 openmpi 64 位时出错
- python - Matplotlib 将 y 轴放在右侧,标签翻转
- firebase - 迭代firestore中的集合和子集合
- python - 使用正则表达式在熊猫数据框中查找格式不正确的数据?
- android - 当 cordova-plugin-local-notification 尝试播放 mp3 文件时,Monaca Android 应用程序崩溃
- c# - UWP - 我无法从 UI 线程编辑 Application 类上的字段
- python - 为什么 python 的“gc.collect()”不能按预期工作?
- flutter - 参数类型'Future
' 不能分配给参数类型 'FutureOr '颤振与飞镖 - python - Python struct.unpack 字节长度问题