“const"和“const"有什么区别?和“最终"Dart 中的关键字? [英] What is the difference between the "const" and "final" keywords in Dart?

查看:39
本文介绍了“const"和“const"有什么区别?和“最终"Dart 中的关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dart 中constfinal 关键字有什么区别?

解决方案

在 dart 的网站上有一个帖子,它解释得很好.

决赛:

<块引用>

最终"意味着单赋值:最终变量或字段必须有一个初始化器.一旦赋值,最终变量的值就不能改变.final 修改变量.


常量:

<块引用>

常量"在 Dart 中具有更复杂和微妙的含义.const 修改.您可以在创建集合时使用它,例如 const [1, 2, 3],以及在构造对象(而不是 new)时,例如 const Point(2, 3).在这里,const 意味着对象的整个深层状态可以在编译时完全确定,并且对象将被冻结并且完全不可变.

<块引用>

Const 对象有几个有趣的属性和限制:

<块引用>

它们必须从可以在编译时计算的数据创建.const 对象无权访问您需要在运行时计算的任何内容.1 + 2 是一个有效的常量表达式,但 new DateTime.now() 不是.

<块引用>

它们是深刻的、可传递的不可变的.如果您有一个包含集合的最终字段,该集合仍然可以是可变的.如果您有一个 const 集合,则其中的所有内容也必须是 const 递归的.

<块引用>

它们被规范化.这有点像字符串实习:对于任何给定的 const 值,无论对 const 表达式求值多少次,都将创建并重用单个 const 对象.


那么,这是什么意思?

常量:
如果您拥有的值是在运行时计算的(例如,new DateTime.now()),您可以为它使用常量.但是,如果该值在编译时已知(const a = 1;),那么您应该使用 const 而不是 final.constfinal 之间还有另外两个很大的区别.首先,如果您使用const,您必须将其声明为static const 而不仅仅是const.其次,如果你有一个 const 集合,它里面的所有东西都在 const 中.如果您有一个 final 集合,那么其中的所有内容都不是 final.

决赛:
final 应该在 const 上使用,如果您在编译时不知道该值,它将在运行时计算/抓取.如果您想要一个无法更改的 HTTP 响应,如果您想从数据库中获取某些内容,或者如果您想从本地文件中读取,请使用 final.任何在编译时不知道的东西都应该是 final 而不是 const.


综上所述,constfinal 都不能重新赋值,但是 final 对象中的字段,只要它们不是 constfinal 本身,可以重新分配(与 const 不同).

What is the difference between const and final keyword in Dart?

解决方案

There is a post on dart's website and it explains it pretty well.

Final:

"final" means single-assignment: a final variable or field must have an initializer. Once assigned a value, a final variable's value cannot be changed. final modifies variables.


Const:

"const" has a meaning that's a bit more complex and subtle in Dart. const modifies values. You can use it when creating collections, like const [1, 2, 3], and when constructing objects (instead of new) like const Point(2, 3). Here, const means that the object's entire deep state can be determined entirely at compile time and that the object will be frozen and completely immutable.

Const objects have a couple of interesting properties and restrictions:

They must be created from data that can be calculated at compile time. A const object does not have access to anything you would need to calculate at runtime. 1 + 2 is a valid const expression, but new DateTime.now() is not.

They are deeply, transitively immutable. If you have a final field containing a collection, that collection can still be mutable. If you have a const collection, everything in it must also be const, recursively.

They are canonicalized. This is sort of like string interning: for any given const value, a single const object will be created and re-used no matter how many times the const expression(s) are evaluated.


So, what does this mean?

Const:
If the value you have is computed at runtime (new DateTime.now(), for example), you can not use a const for it. However, if the value is known at compile time (const a = 1;), then you should use const over final. There are 2 other large differences between const and final. Firstly, if you're using const, you have to declare it as static const rather than just const. Secondly, if you have a const collection, everything inside of that is in const. If you have a final collection, everything inside of that is not final.

Final:
final should be used over const if you don't know the value at compile time, and it will be calculated/grabbed at runtime. If you want an HTTP response that can't be changed, if you want to get something from a database, or if you want to read from a local file, use final. Anything that isn't known at compile time should be final over const.


With all of that being said, both const and final cannot be reassigned, but fields in a final object, as long as they aren't const or final themselves, can be reassigned (unlike const).

这篇关于“const"和“const"有什么区别?和“最终"Dart 中的关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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