簡體   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