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

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

问题描述

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

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

推荐答案

在dart网站上有一篇帖子,它很好地解释了这一点.

最终:

最终"表示单次分配:最终变量或字段必须具有初始化程序.一旦分配了值,最终变量的值就无法更改. 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"的含义在Dart中更加复杂和微妙. const修改.您可以在创建集合(例如const [1、2、3])以及构造对象(而不是new)(例如const Point(2,3))时使用它.在这里,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:

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

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.

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

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.

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

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.


那是什么意思?

常量:
如果您拥有的值是在运行时计算的(例如,new DateTime.now()),则可以为其使用const.但是,如果在编译时知道该值(const a = 1;),则应在final上使用const. constfinal之间还有2个其他大差异.首先,如果您使用的是const,则必须将其声明为static const,而不只是const.其次,如果您有一个const集合,则其中的所有内容都在const中.如果您有一个final集合,则其中的所有内容不是 final.


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的值,则应在const上使用final,并且将在运行时计算/获取该值.如果您想要一个无法更改的HTTP响应,或者想要从数据库中获取内容,或者想要读取本地文件,请使用final.在编译时未知的所有内容应该是final而不是const.

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.

话虽如此,constfinal都不能重新分配,但是final对象中的字段,只要它们不是constfinal,都可以重新分配(与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, can be reassigned (unlike const).

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

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