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 中演示包名冲突


基础教程推荐
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01