C#中的命名空间与Java和Python中的导入 [英] Namespaces in C# vs imports in Java and Python

查看:195
本文介绍了C#中的命名空间与Java和Python中的导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java和Python世界中,您查看源文件并了解所有导入的来源(即您知道在哪个文件中定义了导入的类)。例如:

In the Java and Python world, you look at a source file and know where all the imports come from (i.e. you know in which file the imported classes are defined). For example:

在Java中:

import javafoo.Bar;

public class MyClass {
    private Bar myBar = new Bar();
}

您会立即看到Bar-class是从javafoo导入的。因此,Bar在 /javafoo/Bar.java中声明

You immediately see that the Bar-class is imported from javafoo. So, Bar is declared in /javafoo/Bar.java

在Python中

import pythonbaz
from pythonfoo import Bar

my_bar = Bar()
my_other = pythonbaz.Other()

这里很明显Bar来自pythonfoo包,而Other显然来自pythonbaz。

Here, it is clear that Bar comes from the pythonfoo package and Other is obviously from pythonbaz.

在C#中(如果我错了,请纠正我):

In C# (correct me if I'm wrong):

using foo
using baz
using anothernamespace
...

public class MyClass
{
    private Bar myBar = new Bar();
}

两个问题:

1)我怎么知道宣布Bar-class的地方?它来自命名空间 foo ,还是 bar ,或 anothernamespace ? (编辑:没有使用Visual Studio)

Two questions:
1) How do I know where the Bar-class is declared? Does it come from the namespace foo, or bar, or anothernamespace? (edit: without using Visual Studio)

2)在Java中,包名称对应于目录名称(或者,它非常强大惯例)。因此,当您看到一个类来自哪个包时,您就知道它在文件系统中的目录。

2) In Java, the package names correspond to directory names (or, it is a very strong convention). Thus, when you see which package a class comes from, you know its directory in the file system.

在C#中,似乎没有这样的命名空间约定或者我错过了什么?那么,我如何知道要查看的目录和文件(没有Visual Studio)? (在确定该类来自哪个命名空间之后)。

In C#, there does not seem to be such a convention for namespaces, or am I missing something? So, how do I know which directory and file to look in (without Visual Studio)? (after figuring out which namespace the class came from).

编辑澄清:我知道Python和/或Java允许通配符导入,但是那些语言中的文化对他们不满(至少在Python中,在Java中我不确定)。此外,在Java IDE中通常可以帮助您创建最小的导入(如下面的Mchl。评论)

Edit clarification: I am aware that Python and/or Java allow wildcard imports, but the 'culture' in those languages frowns upon them (at least in Python, in Java I'm not sure). Also, in Java IDEs usually help you create minimal imports (as Mchl. commented below)

推荐答案

1)嗯,你可以做在Java中也是如此:

1) Well, you can do the same thing in Java too:

import java.util.*;
import java.io.*;

...

InputStream x = ...;

InputStream 来自 java.util java.io ?当然,您可以选择不使用该功能。

Does InputStream come from java.util or java.io? Of course, you can choose not to use that feature.

现在,在理论中我意识到这意味着当您查看文本时编辑器,你无法分辨C#中的类型来源......但在实践中,我并不认为这是一个问题。您实际查看代码并且不能使用Visual Studio的频率是多少?

Now, in theory I realise this means when you're looking with a text editor, you can't tell where the types come from in C#... but in practice, I don't find that to be a problem. How often are you actually looking at code and can't use Visual Studio?

2)您也可以在.NET中使用相同的约定当然 - 我也是这样,虽然我没有空目录上链...所以如果我创建一个默认名称空间为XY的项目,那么 XYFoo 将在 Foo.cs 中,XYZBar将在 Z\Bar.cs

2) You can use the same convention in .NET too, of course - and I do, although I don't have empty directories going up the chain... so if I'm creating a project with a default namespace of X.Y, then X.Y.Foo would be in Foo.cs, and X.Y.Z.Bar would be in Z\Bar.cs

这也是Visual Studio默认执行的操作 - 如果您创建子文件夹,它将使用基于项目默认值和文件夹结构的命名空间创建新类。

That's also what Visual Studio will do by default - if you create a subfolder, it will create new classes using a namespace based on the project default and the folder structure.

当然,您也可以在任何旧文件中声明类型 - 但主要是的人将遵循声明具有相应文件名的类型的常规惯例。在泛型使委托声明更少之前,我曾经有一个 Delegates.cs 文件,其中包含特定命名空间的所有委托声明(而不是拥有一堆单声明文件)但现在这不是一个问题。

Of course, you can also declare types in any old file - but mostly people will follow the normal convention of declaring a type with a corresponding filename. Before generics made delegate declarations rarer, I used to have a Delegates.cs file containing all the delegate declarations for a particular namespace (rather than having a bunch of single-declaration files) but these days that's less of an issue.

这篇关于C#中的命名空间与Java和Python中的导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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