使程序包重定向到另一个程序包 [英] Make package redirect to another package

查看:132
本文介绍了使程序包重定向到另一个程序包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使一个程序包重定向到另一个程序包.例如,我有一个名为"packageA"的软件包和另一个名为"packageB"的软件包,导入packageA会导入packageB.我希望能够做到这一点而无需复制软件包.另外,如果导入了packageA而不是packageB,有什么方法可以运行方法吗?

I want to make a package redirect to another package. For example I have a package called "packageA" and another called "packageB", and importing packageA would import packageB. I want to be able to do this without copying the package. Also, is there any way to run a method if packageA was imported instead of packageB?

推荐答案

让我们看看import语句的工作原理:

Let's look at how the import statement works:

首先,import仅导入类或接口.它从不导入软件包.特别是类似

First of all, import only imports classes or interfaces. It never imports a package. In particular, something like

import java.util.*

导入名为java.util的程序包中的所有类和接口.该软件包只是用于类和接口的容器,因此谈论导入软件包"确实没有任何意义.包本身不是可以编译或运行的任何东西.

imports all the classes and interfaces in the package named java.util. This package is simply a container for the classes and interfaces, so it really doesn't make any sense to talk about "importing a package". A package by itself isn't anything that compiles or runs.

话虽如此,谈论重定向"没有任何意义,因为您将重定向到什么位置?如果要从"packageA"导入类,则直接将其导入.

With that said, it doesn't make any sense to talk about "redirecting" because what are you going to redirect to? If you want to import a class from "packageA", just import it directly.

类似地,从packageA导入一个类可以使您访问该类的方法.由于该类位于packageA中,因此尝试从packageB导入它没有任何意义.即使packageB具有相同名称的类,它们也是两个不同的类.实际上,这是使用Java包的主要原因:避免两个不同的程序员编写的类的名称冲突.

Similarly, importing a class from packageA gives you access to that's classes methods. Since that class lives in packageA, it doesn't make any sense trying to import it from packageB. Even if packageB has a class with the same name, they are two different classes. In fact, this is the primary reason for packages in Java: to avoid name clashes for classes written by two different programmers.

话虽如此,您遇到了重定向导入"要解决的问题吗?

With all that said, what problem have you encountered that you are trying to solve with "redirecting imports"?

这篇关于使程序包重定向到另一个程序包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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