如何为Dart类命名空间? [英] How do you namespace a Dart class?

查看:98
本文介绍了如何为Dart类命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为Dart类创建名称空间?我来自C#背景,在那儿,我只会使用 namespace SampleNamespace {} .

How do you create a namespace for a Dart class? I come from a C# background, where one would just use namespace SampleNamespace { }.

您如何在Dart中实现相同目标?

How do you achieve the same in Dart?

推荐答案

Dart没有名称空间的概念,但是它具有库.您可以认为一个库在某种程度上等同于名称空间,因为一个库可以由多个文件组成,并且包含多个类和函数.

Dart doesn't have the concept of namespaces, but instead it has libraries. You can consider a library to be sort of equivalent to a namespace, in that a library can be made of multiple files, and contain multiple classes and functions.

Dart中的隐私权也在库中,而不是在类级别上(任何带下划线的前缀对该库都是私有的.)

Privacy in Dart is also at the library, rather than the class level (anything prefixed with an underscore is private to that library).

定义库的示例(使用实用程序库的示例:

An example of defining a library (using the example of a utilities library:

// utilities.dart
library utilities; // being the first statement in the library file

您可以使用 part 关键字使其他文件成为同一库的一部分.零件文件仅用于帮助组织代码.您可以将所有类放在一个库文件中,或将它们拆分为几个零件文件(或零件文件和库文件)-这对执行没有影响.将主库文件放在一个父文件夹中,并将零件文件放在一个 src/文件夹中,在风格上是合理的.

You can make other files part of the same library by using the part keyword. Part files are only used to help organize your code; you can put all your classes in a single library file, or split them among several part files (or part files and the library file) - it has no effect on the execution. It is stylistic to put the main library file in a parent folder, and part files in a src/ folder.

展开示例以显示零件文件.

Expanding the example to show Part files.

// utilities.dart
library utilities;

part "src/string_utils.dart";
part "src/date_utils.dart";

然后使用 part of 语句将这些零件文件链接回它们所属的库:

Those part files then link back to the library they are part of by using the part of statement:

// src/string_utils.dart
part of utilities;

// functions and classes
String reverseString(s) => // implementation ....

String _stringBuilder(strings) => // a private (to the library) function, 
                                  // indicated by the leading underscore

//... snip other classes and functions

现在您有了一个包含函数的库,您可以通过导入该库,在其他地方使用该库:

Now that you have a library containing a function, you can make use of that library elsewhere by importing the library:

 // my_app.dart;
 import "path/to/library/utilities.dart";

 main() {
   var reversed = reverseString("Foo");
   // _stringBulider(["a","b"]); // won't work - this function is 
                                 // only visible inside the library
 }

如果您想为库添加别名以避免冲突(您可以在其中导入两个都包含 reverseString()函数的库,请使用 as 关键字:

If you want to alias your library to avoid clashes (where you might import two libraries, both containing a reverseString() function, you use the as keyword:

 // my_app.dart;
 import "path/to/library/utilities.dart";
 import "some/other/utilities.dart" as your_utils;

 main() {
   var reversed = reverseString("Foo"); 
   var your_reversed_string = your_utils.reverseString("Bar");
 }

import语句还使用了由dart的包管理器pub导入的包,因此您还可以在github或其他地方托管您的库,并这样引用您的库:

The import statement also makes use of packages, as imported by pub, Dart's package manager, so you can also host your library on github or elsewhere, and reference your library as so:

 // my_app.dart;
 import "package:utilities/utilities.dart";

 main() {
   var reversed = reverseString("Foo");        
 }

pub依赖项在 pubspec.yaml 文件中定义,该文件告诉pub在何处查找库.您可以在 pub.dartlang.org

The pub dependency is defined in a pubspec.yaml file, which tells pub where to find the library. You can find out more at pub.dartlang.org

请注意,只有库文件可以:

It is important to note that only the library file can:

  • 包含 import 语句.零件文件不能.
  • 包含 library 关键字.零件文件不能.
  • 包含 part 个文件.零件文件不能.
  • contain import statements. Part files cannot.
  • contain the library keyword. Part files cannot.
  • contain part files. Part files cannot.

最后一点是,可运行的应用程序文件可以(可能是)库文件,也可以由零件文件组成

One final point is that a runnable app file can (and is likely to be) a library file, and can also be made of part files

 // my_app.dart;
 library my_app;

 import "package:utilities/utilities.dart";

 part "src/edit_ui.dart";
 part "src/list_ui.dart";
 part "src/foo.dart";

 main() {
   var reversed = reverseString("Foo");    
   showEditUi(); // perhaps defined in edit_ui.dart....?
 }

这篇关于如何为Dart类命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆