什么时候在java中使用单独的包? [英] When to use separate package in java?

查看:55
本文介绍了什么时候在java中使用单独的包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,当我查看源 Java 代码时,我注意到除了默认文件之外还有一些文件放置在另一个包中,但不明白何时或为什么使用这种做法.是否存在必须或不得使用单独包的情况?有人愿意解释吗?

Sometimes when I view source java code I notice some files placed in another package besides the default one but have not understood when or why this practice is used. Are there situations where you must or mustn't use separate packages? Anyone care to explain please?

推荐答案

简短的回答:

当您的项目需要特定的组织或层次结构时,或者当您的框架不允许使用默认包时,请使用包.对于简单的 CS 项目,它可能有点矫枉过正.

The short answer:

Use packages when your project requires a specific organization or hierarchy to it, or when your framework disallows the use of the default package. For simple CS projects, it can be overkill.

包是 Java 识别的文件夹,可让您获得某些特权:

Packages are folders recognized by Java that allow you certain perks:

  • 您可以将两个名称相同的类放在不同的文件夹中,而不会引起冲突.

  • You can have two classes that are named the same that live in different folders, without causing conflicts.

一个常见的例子是java.util.Datejava.sql.Date;根据您在做什么,您可能会同时使用两者.如果这样做,则必须使用完全限定的类名,就像编写 java.util.Date date = new java.util.Date();.

A common example is java.util.Date and java.sql.Date; depending on what you're doing, you may wind up using both. If you do, you'd have to use the fully qualified class name, which is like writing java.util.Date date = new java.util.Date();.

你让你的项目有层次感和组织感;给每个班级一个合理的生活"场所.

You give your project a sense of hierarchy and organization; giving each class a sensible place to "live".

以我当前的项目为例.我决定编写一个元数据解析器来读取 MP3、FLAC、Vorbis 和 AAC 文件.

Take, for example, my current project. I have decided to write a metadata parser that will read MP3, FLAC, Vorbis, and AAC files.

马上,我有四个常用接口:

Right away, I have four common interfaces:

MP3
FLAC
Vorbis
AAC

...然而,这些都是真正的压缩格式(而且 FLAC 是无损的,所以那里没有压缩),所以它们属于传达这一点的地方.

...however, these are all really compression formats (and FLAC is lossless, so there's no compression there), so they belong in a place that conveys that.

name.makoto.format
  - MP3
  - FLAC
  - Vorbis
  - AAC

这一切都很好.但是实际进行解析的类会在哪里?如果它存在于阅读器包中会怎样?

That's all good and dandy. But where would the class that actually does the parsing live? What if it lived in a reader package?

name.makoto
    - reader
        MediaReader
    - format
        MP3
        FLAC
        Vorbis
        AAC

假设现在我想实现那些格式接口.让它们与接口本身处于同一级别是没有意义的,因为无论如何它们只是实际对象的 API.让我们把它移到 impl 包中.

Suppose now I want to implement those format interfaces. Doesn't make sense to have them living at the same level as the interfaces themselves, since those are just an API into what the actual object will be anyway. Let's move that to an impl package.

name.makoto
    - reader
        MediaReader
    - format
        MP3
        FLAC
        Vorbis
        AAC
        - impl
            MP3Impl
            FLACImpl
            VorbisImpl
            AACImpl

我会继续,但从这里开始有点疯狂.

I'd go on, but it gets kind of crazy from here.

我在这里对我的项目有一种层次感和结构感.这是一个相当大的项目,所以我可以从组织中受益.

What I have here is a sense of hierarchy and structure to my project. It's a sizable project, so I could benefit from the organization.

这篇关于什么时候在java中使用单独的包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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