在Dart中,const和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;
}

其他回答

合并@Meyi和@faisal-naseer的答案,并与小程序进行比较。

常量:

Const关键字,用于创建存储编译时常量值的变量。编译时常量值是一个在编译时为常量的值:-)

例如,5是一个编译时常数。而DateTime.now()不是编译时间常量。因为这个方法将返回行在运行时被执行的时间。所以我们不能将DateTime.now()赋值给一个const变量。

const a = 5;
// Uncommenting below statement will cause compile time error.
// Because we can't able to assign a runtime value to a const variable
// const b = DateTime.now();

应在同一行进行初始化。

const a = 5;
// Uncommenting below 2 statement will cause compilation error.
// Because const variable must be initialized at the same line.
// const b;
// b = 6;

以下所有陈述均可接受。

// Without type or var
const a = 5;
// With a type
const int b = 5;
// With var
const var c = 6;

类级别的const变量应该像下面这样初始化。

Class A {
    static const a = 5;
}

实例级const变量不可用。

Class A {
    // Uncommenting below statement will give compilation error.
    // Because const is not possible to be used with instance level 
    // variable.
    // const a = 5;
}

const的另一个主要用途是使对象不可变。要使类对象不可变,我们需要使用const关键字和构造函数,并将所有字段设置为final,如下所述。

Class A {
    final a, b;
    const A(this.a, this.b);
}

void main () {
    // There is no way to change a field of object once it's 
    // initialized.
    const immutableObja = const A(5, 6);
    // Uncommenting below statement will give compilation error.
    // Because you are trying to reinitialize a const variable
    // with other value
    // immutableObja = const A(7, 9);

    // But the below one is not the same. Because we are mentioning objA 
    // is a variable of a class A. Not const. So we can able to assign
    // another object of class A to objA.
    A objA = const A(8, 9);
    // Below statement is acceptable.
    objA = const A(10, 11);
}

我们可以对列表使用const关键字。

const a = const[] -初始化为const的变量a,它包含一个const对象的列表(例如:,列表应该只包含编译时间常量和不可变对象)。所以我们不能用另一个列表赋值a。

var a = const[] -初始化为var的变量a,其中包含一个const对象列表。所以我们可以将另一个列表赋值给变量a。

Class A {
    final a, b;
    const A(this.a, this.b);
}

class B {
    B(){ // Doing something }
}

void main() {
    const constantListOfInt = const [5, 6, 7,
                 // Uncommenting below statement give compilation error.
                 // Because we are trying to add a runtime value
                 // to a constant list
                 // DateTime.now().millisecondsSinceEpoch
              ];
    const constantListOfConstantObjA = const [
        A(5, 6),
        A(55, 88),
        A(100, 9),
    ];
    // Uncommenting below 2 statements will give compilation error.
    // Because we are trying to reinitialize with a new list.
    // constantListOfInt = [8, 9, 10];
    // constantListOfConstantObjA = const[A(55, 77)];

    // But the following lines are little different. Because we are just
    // trying to assign a list of constant values to a variable. Which 
    // is acceptable
    var variableWithConstantList = const [5, 6, 7];
    variableWithConstantList = const [10, 11, 15];
    var variableOfConstantListOfObjA = const [A(5, 8), A(7, 9), A(10, 4)];
    variableWithConstantList = const [A(9, 10)];
}

最后:

Final关键字还用于使变量保持恒定值。一旦初始化,就不能更改值。

final a = 5;
// Uncommenting below statement will give compilation error.
// Because a is declared as final.
// a = 6;

以下所有陈述均可接受。

// Without type or var
final a = 5;
// With a type
final int b = 5;
// Can't use var along with final keyword. Uncommenting below line cause compilation issue.
// final var c = 6;

能够分配运行时值。

// DateTime.now() will return the time when the line is getting
// executed. Which is a runtime value.
final a = DateTime.now();
var b = 5;
final c = b;

类级final变量必须在同一行中初始化。

Class A {
    static final a = 5;
    static final b = DateTime.now();
}

实例级final变量必须在同一行或在构造函数初始化中初始化。该值将在创建对象时被放入内存中。

Class A {
    final a = 5;
}

// Constructor with a parameter.
Class B {
    final b;
    B(this.b);
}

// Constructor with multiple parameter.
Class C {
    final c;
    C(this.c, int d) {
        // Do something with d
    }
}

void main() {
    A objA = new A();
    B objB = new B(5);
    C objC = new C(5, 6);
}

分配一个列表。

final a = [5, 6, 7, 5.6, A()];
// Uncommenting Below statement will give compilation error.
// Because we are trying to reinitialize the object with another list.
// a = [9.9, 10, B()];

const是一个编译时常量。

final是一个运行时常数。

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

编译时不知道的任何东西都应该在const上使用final。

所有这些答案我都可以用简洁的方式描述。

const list = [1, 2, 3];

变量/标识符和值都是const。Like - const list = const [1,2,3] 这就是为什么他们不允许被重新分配。 很适合全局变量。 可以将其用作类变量,但必须设置为静态。Like - static const list =[1,2,3]。

vs:

final list = [1, 2, 3];

变量/标识符是const,而值不是。Like - const list = [1,2,3] 这就是为什么我们可以执行like - list.add(4)