dart 中的动态和对象有什么区别? [英] What is the difference between dynamic and Object in dart?

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

问题描述

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

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

推荐答案

Dart 编程语言规范,第 3 版指出:

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

动态类型具有针对每个可能的标识符和数量的方法,具有命名参数的所有可能组合.这些方法都有动态作为它们的返回类型,它们的形式参数都具有类型动态.类型 dynamic 具有每个可能的标识符的属性.这些属性都有类型动态.

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 中的动态和对象有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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