繁体   English   中英

Dart中两个变量同名不同Scope时如何指定预期变量?

[英]How to Specify Intended Variable When two Variables Have Same Name and Different Scope in Dart?

假设在 Dart (DartPad) 中我们有:

String a = "I am higher in the scope chain";

void main() {
  String a = "I am lower in the scope chain, but I have the same name";
  print(a);
}

是否有我们可以使用的关键字,以便它打印父 scope 中的变量,例如:

print(parent.a)

要么

print(this.a)

(那些不起作用)。

如果你的文件名是'some_dart_file.dart',你可以这样做

import './some_dart_file.dart' as parent show a;

String a = "I am higher in the scope chain";

void main() {
  String a = "I am lower in the scope chain, but I have the same name";
  print(parent.a);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM