简体   繁体   中英

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 ?

Is the statement import pckg1.pckg2.pckg3.Abc ambiguous as Abc is present in all the three packages.

There's no such thing as a package hierarchy actually.

pckg1.pckg2 knows nothing about and inherits nothing from pckg1 . Indeed, pckg1.pckg2 can exist without there even being a pckg1 . It's basically just a naming convention to help logically order things.

pckg1.pckg2.pckg3.Abc is therefore not ambiguous. It will come from the package that you currently consider the lowest in the hierarchy, pckg3 .

That said, there is no such thing as pckg3 . There is pckg1 , pckg1.pckg2 and pckg1.pckg2.pckg3 but they could just as easily be called cat , banana and magic .

import pckg1.pckg2.pckg3.Abc is not ambiguous. It refer to specific class. In this case Abc from pckg1.pckg2.pckg3 package.

In java there is no package hierarchy. 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).

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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