Demonstrating package names collision in java(在 java 中演示包名冲突)
问题描述
我收到了这个问题作为作业:
包/命名
我们创建了很多包并在其中定义了类和接口.我们还讨论了在命名它们时必须记住的一点.在本次作业中,我们将看到命名的重要性.
We have created lot of packages and defined classes and interfaces in them. We have also discussed the point we have to remember while naming them. In this assignment we will see how important naming is.
请更改您之前分配的包名称,使两个包具有相同的名称,并分析将引发的结果/错误.
Please change the package names in your previous assignment such that two packages have same name and analyze the result/errors that will be thrown.
我的疑问:
我想不出一种方法(场景)来证明所要求的内容.由于 Java 导入是绝对的,因此所描述的情况似乎不可能产生 (IMO).
I am unable to think of a way(scenario) to demonstrate what has been asked. Since Java imports are absolute, the situation described in question seem impossible to produce (IMO).
请帮我演示一下.
提前致谢:)
推荐答案
这在很大程度上取决于您正在运行的代码.
That will depend heavily on the code that you're running.
导致包名冲突的唯一方法是在类路径中放置两个单独的 jar,它们都包含同一个包中的类.如果没有一个类名冲突,那么就没有冲突.如果某些类名确实冲突,那么 JVM 将尝试从类路径中较早出现的 jar 加载它们.有的类只在后面的jar中,而后面的jar中的类使用了前面jar中也使用了名字的类,就会出现错误.错误的性质取决于使用的类型.
The only way to cause a package name conflict is to put two separate jars on your classpath that both contain classes in the same package. If none of the class names conflict, then there is no conflict. If some class names do conflict, then the JVM will try to load them from the jar that comes earlier in the classpath. Errors will occur when some classes are only in the later jar, and the classes in the later jar use classes whose names are also used in the earlier jar. The nature of the error depends on the type of use.
(我应该澄清一下,这个答案适用于 Pure Java,并且与任何特定 IDE 或构建系统如何为项目生成 jars 无关.)
(I should clarify that this answer applies to Pure Java, and has no relation to how any particular IDE or build system might generate jars for a project.)
这篇关于在 java 中演示包名冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 java 中演示包名冲突


- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01