在 Java 文件中定义包的目的是什么? [英] What is the purpose of defining a package in a Java file?

查看:21
本文介绍了在 Java 文件中定义包的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手,刚刚知道如果我定义说的话

I am a newbie and just learned that if I define say

package my.first.group.here;
...

那么这个包中的Java文件将被放在my/first/group/here目录下.

then the Java files that are in this package will be placed under my/first/group/here directory.

将一些 Java 文件放在一个包中的主要目的是什么?另外,如果我选择采用这个,我应该如何将它们分组?

What is the main purpose of putting some Java files in a package? Also, if I choose to adopt this, how should I group them?

谢谢

对于任何可能再次遇到相同问题的人,我刚刚发现 这个关于软件包的教程来自 Sun.

For anyone who might have the same question again, I just found this tutorial on packages from Sun.

推荐答案

让我们从Java 包"的定义开始,如 维基百科文章:

Let's start with the definition of a "Java package", as described in the Wikipedia article:

Java 包是一种机制将 Java 类组织成类似于模块的命名空间模数.Java包可以存储在称为 JAR 文件的压缩文件,允许类下载速度更快一组而不是一次一个.程序员通常还使用用于组织类的包到同一类别或提供类似的功能.

A Java package is a mechanism for organizing Java classes into namespaces similar to the modules of Modula. Java packages can be stored in compressed files called JAR files, allowing classes to download faster as a group rather than one at a time. Programmers also typically use packages to organize classes belonging to the same category or providing similar functionality.

因此,基于此,Java 中的包只是一种用于组织类和防止类名冲突的机制.您可以随意命名它们,但 Sun 发布了一些 命名约定 您在命名包时应该使用:

So based on that, packages in Java are simply a mechanism used to organize classes and prevent class name collisions. You can name them anything you wish, but Sun has published some naming conventions that you should use when naming packages:

唯一包名的前缀是始终以全小写 ASCII 书写字母,应该是其中之一顶级域名,目前为com,edu、gov、mil、net、org 或其中之一英文两字母代码识别ISO 标准中指定的国家3166, 1981.

The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.

包的后续组件名称因人而异组织自己的内部命名公约.这样的约定可能指定某个目录名称组成部分是部门,部门,项目、机器或登录名.

Subsequent components of the package name vary according to an organization's own internal naming conventions. Such conventions might specify that certain directory name components be division, department, project, machine, or login names.

示例:

  • com.sun.eng

  • com.sun.eng

com.apple.quicktime.v2

com.apple.quicktime.v2

edu.cmu.cs.bovik.cheese

edu.cmu.cs.bovik.cheese

这篇关于在 Java 文件中定义包的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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