java 我必须在每节课中使用“包”这个词吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17797941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do I have to use "package" term in every class?
提问by Yusuf
Firstly, I'm trying to learn Java with Java, A Beginner's Guide, 4th Edition. I use Ubuntu as my OS and Netbeans as my IDE. I need to create a project to create a class when using Netbeans.
首先,我正在尝试使用Java学习 Java ,初学者指南,第 4 版。我使用 Ubuntu 作为我的操作系统和 Netbeans 作为我的 IDE。使用 Netbeans 时,我需要创建一个项目来创建一个类。
Here is my hello world app.
这是我的 hello world 应用程序。
class First{
public static void main(String args[])
{
System.out.println("Hello!");
}
}
But this returns a lot of errors. When I put package first;
line to top line of my Java class it runs. But, the book isn't using package term. Is this a rule of Netbeans, or what I need to know about this?
但这会返回很多错误。当我将package first;
line放在Java 类的顶行时,它会运行。但是,这本书没有使用包术语。这是 Netbeans 的规则,还是我需要了解的内容?
回答by gobernador
You never needto put a class in a package. However, it is almost always a good idea. This is something that Netbeans aggressively tries to encourage you to do.
你永远不需要把一个类放在一个包中。然而,这几乎总是一个好主意。这是 Netbeans 积极鼓励您去做的事情。
For small projects, using the default package (that is, a file without a package
statement) is OK. However, once your project grows and starts adding external libraries, bad things can happen. What if someone else decided to use the default package and happened to have an identically-named class? Now, since you're both in the same package, collisions can occur!
对于小项目,使用默认包(即没有package
声明的文件)是可以的。但是,一旦您的项目增长并开始添加外部库,就会发生不好的事情。如果其他人决定使用默认包并且碰巧有一个同名的类怎么办?现在,因为你们都在同一个包中,所以可能会发生冲突!
Your file structure should also reflect your package. That is, a file in package com.myurl.helloworld
should be located in a folder called com/myurl/helloworld
. This is for the same reasons as above.
您的文件结构也应该反映您的包。也就是说,文件 inpackage com.myurl.helloworld
应位于名为com/myurl/helloworld
. 这是出于与上述相同的原因。
Also, and you probably haven't gotten here in your studies, you cannot import classes from the default package, or use the package-private visibility modifier in a default package class.
此外,您可能还没有在学习中到达这里,您不能从默认包中导入类,或在默认包类中使用包私有可见性修饰符。
回答by Scientious
That's because the author of Java, A Beginner's Guide, 4th Editionmost likely used the "default package". This way, you don't have to include any package. A package is a namespace declaration.
那是因为Java, A Beginner's Guide, 4th Edition 的作者很可能使用了“默认包”。这样,您就不必包含任何包。包是命名空间声明。
What the heck is a namespace declaration!?A namespace declaration is simply a package which is made to organize your classes. For an instance, if you're going to have multiple classes for, let's say your GUI, and multiple classes for algorithms, blending them together is always a bit confusing. Sorting them in different packages, however is a superior solution.
命名空间声明是什么鬼!?命名空间声明只是一个用于组织类的包。举个例子,如果你有多个类,比如你的 GUI,还有多个算法类,将它们混合在一起总是有点令人困惑。然而,将它们分类在不同的包中是一个很好的解决方案。
There is also a naming convention which you should follow if other people are going to look at your code. Packages should be named after a top-level domain. I tend to create SourceForge projects and then I end up with something like this:
如果其他人要查看您的代码,您还应该遵循一个命名约定。包应该以顶级域命名。我倾向于创建 SourceForge 项目,然后我最终得到这样的结果:
net.sourceforge.softwarename.security
, net.sourceforge.softwarename.gui
, etc...
net.sourceforge.softwarename.security
, net.sourceforge.softwarename.gui
, 等等...
Also note that you should never use upper case when naming your package. More info here.
另请注意,在命名包时不应使用大写。更多信息在这里。
You're going to encounter lots of situations like these when learning to programming. They're all a part of the game. You'll just have to figure out a bit by yourself.
在学习编程时,您会遇到很多这样的情况。他们都是比赛的一部分。你只需要自己弄清楚一点。
The best I can do for you is to recommend Eclipse. Also, when learning Java, I would suggest that you do not use an IDE at ALL! That's because you'll learn to code independently. It's up to you, though.
我能为您做的最好的事情就是推荐Eclipse。另外,在学习 Java 时,我建议您根本不要使用 IDE!那是因为您将学会独立编码。不过,这取决于你。
回答by tokhi
Package represents a directory that contains related group of classes and interfaces.
包表示包含相关类和接口组的目录。
A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer. You might keep HTML pages in one folder, images in another, and scripts or applications in yet another. Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.
包是组织一组相关类和接口的命名空间。从概念上讲,您可以将包视为类似于计算机上的不同文件夹。您可以将 HTML 页面保存在一个文件夹中,将图像保存在另一个文件夹中,并将脚本或应用程序保存在另一个文件夹中。因为用 Java 编程语言编写的软件可以由成百上千个单独的类组成,所以通过将相关的类和接口放入包中来保持组织有序是有意义的。
Below you can find some good discussions regarding java packages:
下面你可以找到一些关于 java 包的很好的讨论:
回答by Eric Eugene Herring
No you don't need to put in a package into your class UNLESS you are going to import it to another class that will be in it's own file. This is where protected type variables come in when you don't want to make them priviate but only want the subclasses (or child classes) access to them. You are also missing your public statement for your class so it should look like
不,您不需要将包放入您的类中,除非您要将它导入到另一个类中,该类将在它自己的文件中。当您不想将它们私有化而只想让子类(或子类)访问它们时,这就是受保护类型变量的用武之地。您还缺少您班级的公开声明,因此它应该看起来像
public class First{
public static void main(String[] args){
System.out.println("Hello!");
}
}