热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Dart中''const''和''final''关键字有什么区别?

如何解决《Dart中''const''和''final''关键字有什么区别?》经验,为你挑选了4个好方法。

是什么区别constfinal关键字飞镖?



1> meyi..:

我在dart的网站上发现了这篇文章,它解释得非常好.

https://news.dartlang.org/2012/06/const-static-final-oh-my.html

最后:

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

常数:

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

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

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

它们是深刻的,过渡性的一成不变的.如果您有包含集合的final字段,则该集合仍然可以变为可变.如果你有一个const集合,那么其中的所有东西也必须是递归的const.

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


const关键字用于表示编译时常量。使用const关键字声明的变量是隐式最终的。
@CopsOnRoad,您可以查看此视频[Dart Const vs Final](https://www.youtube.com/watch?v=5pe505l9_nE)

2> Mahendran Sa..:

合并@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 [] - a 初始化constconst的变量,其中包含对象列表(即,列表应仅包含编译时常量和不可变对象).所以我们无法分配a另一个列表.

var a = const [] - a 初始化为var包含列表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;
// With var
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;

类级最终变量必须在同一行中初始化.

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

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

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



3> Faisal Nasee..:

通过@Meyi扩展答案

最终变量只能设置一次,并在访问时初始化.(例如,如果您使用值,则从下面的代码部分biggestNumberOndice开始,将初始化该值并分配内存).

const本质上是最终的,但主要区别在于它的编译时间常量在编译期间初始化,即使你不使用它的值,它也会被初始化并占用内存空间.

类中的变量可以是final但不是常量,如果你想在类级别使用常量,则使其成为静态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;
}


我认为一个更好的方式来问这个问题是何时更喜欢运行时初始化而不是编译时初始化....

4> Haroun Hajem..:

康斯特

值必须在被称为编译时const birth = "2008/12/26"。初始化后无法更改


最后

值必须在被称为运行时final birth = getBirthFromDB()。初始化后无法更改


推荐阅读
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 预备知识可参考我整理的博客Windows编程之线程:https:www.cnblogs.comZhuSenlinp16662075.htmlWindows编程之线程同步:https ... [详细]
  • Introduction(简介)Forbeingapowerfulobject-orientedprogramminglanguage,Cisuseda ... [详细]
  • Nginx使用AWStats日志分析的步骤及注意事项
    本文介绍了在Centos7操作系统上使用Nginx和AWStats进行日志分析的步骤和注意事项。通过AWStats可以统计网站的访问量、IP地址、操作系统、浏览器等信息,并提供精确到每月、每日、每小时的数据。在部署AWStats之前需要确认服务器上已经安装了Perl环境,并进行DNS解析。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • 原文地址:https:www.cnblogs.combaoyipSpringBoot_YML.html1.在springboot中,有两种配置文件,一种 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • 本文介绍了计算机网络的定义和通信流程,包括客户端编译文件、二进制转换、三层路由设备等。同时,还介绍了计算机网络中常用的关键词,如MAC地址和IP地址。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • Android系统源码分析Zygote和SystemServer启动过程详解
    本文详细解析了Android系统源码中Zygote和SystemServer的启动过程。首先介绍了系统framework层启动的内容,帮助理解四大组件的启动和管理过程。接着介绍了AMS、PMS等系统服务的作用和调用方式。然后详细分析了Zygote的启动过程,解释了Zygote在Android启动过程中的决定作用。最后通过时序图展示了整个过程。 ... [详细]
  • 本文介绍了互联网思维中的三个段子,涵盖了餐饮行业、淘品牌和创业企业的案例。通过这些案例,探讨了互联网思维的九大分类和十九条法则。其中包括雕爷牛腩餐厅的成功经验,三只松鼠淘品牌的包装策略以及一家创业企业的销售额增长情况。这些案例展示了互联网思维在不同领域的应用和成功之道。 ... [详细]
  • 本文整理了315道Python基础题目及答案,帮助读者检验学习成果。文章介绍了学习Python的途径、Python与其他编程语言的对比、解释型和编译型编程语言的简述、Python解释器的种类和特点、位和字节的关系、以及至少5个PEP8规范。对于想要检验自己学习成果的读者,这些题目将是一个不错的选择。请注意,答案在视频中,本文不提供答案。 ... [详细]
  • {moduleinfo:{card_count:[{count_phone:1,count:1}],search_count:[{count_phone:4 ... [详细]
author-avatar
fanhua1
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有