在 java 中演示包名冲突 [英] Demonstrating package names collision in java

查看:19
本文介绍了在 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 中演示包名冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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