简体   繁体   English

从包的层次结构导入类

[英]Import a class from a hierarchy of packages

If there's a hierarchy of packages(ie pckg1.pckg2.pckg3 ) and each of them has a same class(ie Abc), then how do i import the class Abc from pckg1 ? 如果有一个包的层次结构(即pckg1.pckg2.pckg3 ),并且每个包具有相同的类(即Abc),那么我如何从pckg1导入类Abc?

Is the statement import pckg1.pckg2.pckg3.Abc ambiguous as Abc is present in all the three packages. 语句import pckg1.pckg2.pckg3.Abc模棱两可,因为这三个软件包中都存在Abc

There's no such thing as a package hierarchy actually. 实际上没有包层次结构之类的东西。

pckg1.pckg2 knows nothing about and inherits nothing from pckg1 . pckg1.pckg2一无所知,也不从pckg1继承任何pckg1 Indeed, pckg1.pckg2 can exist without there even being a pckg1 . 实际上, pckg1.pckg2可以存在,甚至没有pckg1 It's basically just a naming convention to help logically order things. 基本上,这只是一种命名约定,可以帮助您逻辑上排序。

pckg1.pckg2.pckg3.Abc is therefore not ambiguous. pckg1.pckg2.pckg3.Abc因此不明确。 It will come from the package that you currently consider the lowest in the hierarchy, pckg3 . 它来自您当前认为层次结构中最低的软件包pckg3

That said, there is no such thing as pckg3 . 就是说,没有pckg3这样的东西。 There is pckg1 , pckg1.pckg2 and pckg1.pckg2.pckg3 but they could just as easily be called cat , banana and magic . pckg1pckg1.pckg2pckg1.pckg2.pckg3但是它们很容易被称为catbananamagic

import pckg1.pckg2.pckg3.Abc is not ambiguous. import pckg1.pckg2.pckg3.Abc不明确。 It refer to specific class. 它指的是特定类别。 In this case Abc from pckg1.pckg2.pckg3 package. 在这种情况下,来自pckg1.pckg2.pckg3包的Abc

In java there is no package hierarchy. 在Java中,没有包层次结构。 It is only look like pckg1.pckg2 and pckg1.pckg2.pckg3 are related, but in fact there are no relation between them (except file structure where .class files are stored, but it is not part of language). 看起来好像pckg1.pckg2pckg1.pckg2.pckg3是相关的,但实际上它们之间没有关系(存储.class文件的文件结构除外,但这不是语言的一部分)。

import pckg1.Abc;

将软件包视为Windows文件夹。

As mentioned you would: 如前所述,您将:

import pckg1.Abc;

But I would also do the full package path when defining variables to avoid ambiguity. 但是在定义变量时,我也将使用完整的包路径,以避免产生歧义。 For example: 例如:

pckg1.Abc abc = new pckg1.Abc();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM