在Dart中,const和final关键字之间有什么区别?
当前回答
如果你来自c++,那么Dart中的const在c++中是constexpr,而Dart中的final在c++中是const。
以上内容仅适用于基本类型。 然而在Dart中,标记为final的对象在其成员方面是可变的。
其他回答
通过@Meyi扩展答案
final variable can only be set once and it is initialized when accessed.(for example from code section below if you use the value of biggestNumberOndice only then the value will be initialized and memory will be assigned). const is internally final in nature but the main difference is that its compile time constant which is initialized during compilation even if you don't use its value it will get initialized and will take space in memory. Variable from classes can be final but not constant and if you want a constant at class level make it static const.
代码:
void main() {
// final demonstration
final biggestNumberOndice = '6';
// biggestNumberOndice = '8'; // Throws an error for reinitialization
// const
const smallestNumberOnDice = 1;
}
class TestClass {
final biggestNumberOndice = '6';
//const smallestNumberOnDice = 1; //Throws an error
//Error . only static fields can be declared as constants.
static const smallestNumberOnDice = 1;
}
你不能使用final对象初始化const对象。例如:
final myConst = 1;
const myFinal = 2;
final a = myConst; // possible
final b = myFinal; // possible
const c = myConst; // this is not possible
const d = myFinal; // possible
Const表示它的初始值必须是固定的,不能是动态值;
Final意味着它的初始值必须是固定的,但可以是一个动态值,等于具有固定值的var。
代码演示
常量
void main() {
const sum = 1 + 2;
// ✅ const can not change its value
print("sum = ${sum}");
// ⚠️ Const variables must be initialized with a constant value.
const time = new DateTime.now();
// ❌ Error: New expression is not a constant expression.
print("time = ${time}");
}
最后
// new DateTime.now();
// dynamic timestamp
void main() {
final sum = 1 + 2;
// ✅ final can not change its value
print("sum = ${sum}");
final time = new DateTime.now();
// ✅ final === var with fixed value
print("time = ${time}");
}
截图
refs
https://dart.dev/guides/language/language-tour#final-and-const
final和const都防止变量被重新赋值(类似于Java中的final或JavaScript中的const)。
这种差异与内存的分配方式有关。在运行时为final变量分配内存,在编译时为const变量分配内存。最后一个修饰符应该是更常用的,因为许多程序变量不需要任何内存,因为程序逻辑不会调用它们进行初始化。对于一个const变量,你基本上是在告诉计算机,“嘿,我需要这个变量的内存,因为我知道我将会需要它。”
以这种方式思考它们可以更容易地理解它们在语法用法上的差异。主要是final变量可以是实例变量,而const必须是类上的静态变量。这是因为实例变量是在运行时创建的,而const变量(根据定义)不是。因此,类上的const变量必须是静态的,这意味着类上存在该变量的单个副本,而不管该类是否实例化。
这段视频相当简单地解释了这一点: https://www.youtube.com/watch?v=9ZZL3iyf4Vk
本文将更深入地讨论并解释两者之间非常重要的语义差异,即final修改变量而const修改值,这本质上归结为只能初始化编译时可派生的const值。
https://news.dartlang.org/2012/06/const-static-final-oh-my.html
常量
Value必须在编译时已知,const birthday = "2008/12/25"初始化后不能更改。
最后
值必须在运行时已知,最终生日= getBirthDateFromDB()初始化后不能更改。
推荐文章
- 错误地使用父数据小部件。扩展小部件必须放置在flex小部件中
- 颤振给容器圆形边界
- Flutter: RenderBox没有布局
- 比较在Java中声明为final的==字符串
- 颤振插件未安装错误;当运行'扑动医生'时
- 我如何“休眠”Dart程序
- 在Flutter app上检查是否有网络连接
- 如何选择多行填充常量?
- Flutter and google_sign_in plugin: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10:, null)
- 如何在扑动中格式化日期时间
- Ruby on Rails:哪里定义全局常量?
- 在Flutter中Column的子元素之间的空间
- 在c++中,是通过值传递更好,还是通过引用到const传递更好?
- Visual Studio代码- URI的目标不存在" package:flutter/material.dart "
- 声明一个常量数组