在Dart中,const和final关键字之间有什么区别?


当前回答

合并@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()];

其他回答

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

最后:

"final"表示单赋值:final变量或字段必须有初始化式。一旦赋值,最终变量的值就不能更改。Final修改变量。


常量:

“const”在Dart中有一个更复杂和微妙的含义。Const用于修改值。你可以在创建集合时使用它,比如const[1,2,3],也可以在构造对象(而不是new)时使用它,比如const Point(2,3)。在这里,const意味着对象的整个深度状态可以完全在编译时确定,对象将被冻结并且完全不可变。

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

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

它们是深刻的、传递的不可变的。如果final字段包含一个集合,则该集合仍然可以是可变的。如果你有一个const集合,它里面的所有东西也必须是const,递归地。

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


那么,这意味着什么呢?

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 inside a class, 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。编译时不知道的任何东西都应该在const上使用final。


话虽如此,const和final都不能被重赋,但final对象中的字段,只要它们本身不是const或final,就可以被重赋(不像const)。

常量

Value必须在编译时已知,const birthday = "2008/12/25"初始化后不能更改。


最后

值必须在运行时已知,最终生日= getBirthDateFromDB()初始化后不能更改。

合并@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()];

简单的单词:

常量

值必须在编译时已知,即来自内部文件的值。

示例:API键,应用程序支持的语言或helper文件中的任何变量,基本上是应用程序附带的任何东西。

最后

值必须在运行时已知。

它可以是上面的数据,也可以是设备信息,当应用程序启动时将被检查,或者当应用程序启动时从API或服务器加载的数据,但在应用程序准备使用之前,即你需要检查用户是否登录,你的应用程序将从服务器加载或检查会话令牌。

你不能使用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