Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
可以使用dart中的常量将最终实例变量重新分配给其他值_Dart - Fatal编程技术网

可以使用dart中的常量将最终实例变量重新分配给其他值

可以使用dart中的常量将最终实例变量重新分配给其他值,dart,Dart,有人知道为什么下面的代码在dart中工作吗final关键字用于定义常量变量。但是下面的代码工作起来没有什么不同。如果我们用不同的值使用const,它工作正常,不会出现错误 void main() { ExampleFinal exampleFinal = new ExampleFinal(); } class ExampleFinal() { final a = 5; ExampleFinal() { // The below statement wil

有人知道为什么下面的代码在dart中工作吗
final
关键字用于定义常量变量。但是下面的代码工作起来没有什么不同。如果我们用不同的值使用
const
,它工作正常,不会出现错误

void main() {
    ExampleFinal exampleFinal = new ExampleFinal();
}

class ExampleFinal() {
    final a = 5;
    ExampleFinal() {
        // The below statement will not create any error. 
        // But if you are remove const in below line it'll show a compile time error.
        const a = 6;
        print(a); // Prints 6
    }
}
它是dart中的一个bug还是一个特性?文件中也没有提到类似的内容

const a = 6;
创建一个新变量,使
最终a=5阴影化

这是可能的,因为
{…}
在构造函数主体中创建了一个新的作用域

如果在构造函数末尾添加

print(this.a); 

它将打印
5

看起来很有趣。但它也可能混淆。文件中也没有提到这样的事情!已经有一段时间没有检查文档了。变量前的类型和/或
var
const
final
始终创建一个新变量。在同一范围内,如果存在名称冲突,则会出现错误。我使用的大多数语言的行为都非常相似。