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

查看:141
本文介绍了在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?

谢谢

编辑:对于可能再次遇到相同问题的人,我刚刚找到关于软件包的本教程

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类到
命名空间的机制,类似于
Modula的模块。 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
字母写的,应该是
top-中的一个级别域名,目前为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天全站免登陆