Dart中的Dynamic和Object之间有什么区别? [英] What is the difference between dynamic and Object in dart?

查看:178
本文介绍了Dart中的Dynamic和Object之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它们似乎都可以在相同的情况下使用.在类型检查等方面有不同的表示形式或不同的细微之处吗?

They both seem like they can be used in identical cases. Is there a different representation or different subtleties in type checking, etc?

推荐答案

动态类型部分/ECMA-ST/ECMA-408.pdf"rel =" noreferrer> Dart编程语言规范,第三版指出:

The section Type dynamic from the Dart Programming Language Specification, 3rd Edition states :

动态类型具有用于每种可能的标识符和arity的方法, 命名参数的所有可能组合.这些方法都有动态 作为其返回类型,其形式参数均具有动态类型. 动态类型具有每个可能的标识符的属性.这些属性全部 具有动态类型.

Type dynamic has methods for every possible identifier and arity, with every possible combination of named parameters. These methods all have dynamic as their return type, and their formal parameters all have type dynamic. Type dynamic has properties for every possible identifier. These properties all have type dynamic.

这意味着通过在dynamic类型变量上调用任何方法都不会收到警告.对于类型为Object的变量,情况并非如此.例如:

That means you will not get warnings by calling any method on a dynamic typed variable. That will not be the case with a variable typed as Object. For instance:

dynamic a;
Object b;

main() {
  a = "";
  b = "";
  printLengths();
}

printLengths() {
  // no warning
  print(a.length);

  // warning:
  // The getter 'length' is not defined for the class 'Object'
  print(b.length);
}

在运行时,我认为您应该不会有任何区别.

At runtime, I think, you shouldn't see any difference.

这篇关于Dart中的Dynamic和Object之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆