在 Java 文件中定义包的目的是什么?

What is the purpose of defining a package in a Java file?(在 Java 文件中定义包的目的是什么?)
本文介绍了在 Java 文件中定义包的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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 文件中定义包的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Sending a keyboard event from java to any application (on-screen-keyboard)(将键盘事件从 java 发送到任何应用程序(屏幕键盘))
How to make JComboBox selected item not changed when scrolling through its popuplist using keyboard(使用键盘滚动其弹出列表时如何使 JComboBox 所选项目不更改)
Capturing keystrokes without focus(在没有焦点的情况下捕获击键)
How can I position a layout right above the android on-screen keyboard?(如何将布局放置在 android 屏幕键盘的正上方?)
How to check for key being held down on startup in Java(如何检查在Java中启动时按住的键)
Android - Get keyboard key press(Android - 获取键盘按键)