Java中的嵌套包 [英] Nested packages in Java

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

问题描述

首先,我想创建一个具有以下结构的自定义用户库:

Firstly I want to create a custom user library with the following structure:

src:


  • LibA.pack1

    • ClassName0.java


    • ClassName1.java

    我对这个没问题。后来我想将这个库导入另一个项目并调用

    I have no problem with this one. Later I want to import this library into the other project and call

    import LibA.*;
    

    (使用pack1和pack2这两个类),
    将根据需要失败全名,即

    (to use both classes of pack1 and pack2), which will fail as it requires full name, i.e.

    import LibA.pack1;
    

    如何一次导入整个库才能使用pack1和pack2这两个类?

    How can I import the whole library at once to be able to use both classes of pack1 and pack2?

    Ps它绝对不叫嵌套包,但我不知道如何调用它。
    P.p.s.如果重要的话,我正在使用Eclipse。

    P.s. It's definitely not called "nested packages" but I have no idea how to call this. P.p.s. I'm using Eclipse if it matters.

    提前致谢:)

    推荐答案

    你不能,因为在java中没有嵌套包这样的东西。您必须明确导入这两个包。

    You can't, since there is no such thing as nested packages in java. You must import both packages explicitly.

    import LibA.pack1.*;
    import LibA.pack2.*;
    

    LibA.pack1 与任何相关到 LibA.pack2 的方式,它们都与 LibA 包无关,所以如果 LibA 有你想要导入的其他类,你需要第三次导入:

    LibA.pack1 is not related in any way to LibA.pack2, and both of them have no relation to LibA package, so if LibA has additional classes you wish to import, you'll need a 3rd import :

    import LibA.*;
    




    包的显式层次结构

    Apparent Hierarchies of Packages

    首先,包显示为是等级的,但他们不是。例如,Java API包括java.awt包,java.awt.color包,java.awt.font包以及许多以java.awt开头的包。但是,java.awt.color包中包含java.awt.color包,java.awt.font包和其他java.awt.xxxx包。前缀java.awt(Java抽象窗口工具包)用于许多相关的包,以使关系明显,但不显示包含。

    At first, packages appear to be hierarchical, but they are not. For example, the Java API includes a java.awt package, a java.awt.color package, a java.awt.font package, and many others that begin with java.awt. However, the java.awt.color package, the java.awt.font package, and other java.awt.xxxx packages are not included in the java.awt package. The prefix java.awt (the Java Abstract Window Toolkit) is used for a number of related packages to make the relationship evident, but not to show inclusion.

    导入java。 awt。*导入java.awt包中的所有类型,但它不导入java.awt.color,java.awt.font或任何其他java.awt.xxxx包。如果您计划使用java.awt.color中的类和其他类型以及java.awt中的类,则必须导入包含所有文件的包:

    Importing java.awt.* imports all of the types in the java.awt package, but it does not import java.awt.color, java.awt.font, or any other java.awt.xxxx packages. If you plan to use the classes and other types in java.awt.color as well as those in java.awt, you must import both packages with all their files:

    import java.awt.*;
    import java.awt.color.*;
    


    这篇关于Java中的嵌套包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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