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

查看:97
本文介绍了在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).

请帮助我演示一下这件事.

Please help me to demonstrate this thing.

先谢谢您了:)

推荐答案

这将在很大程度上取决于您正在运行的代码.

That will depend heavily on the code that you're running.

引起程序包名称冲突的唯一方法是将两个单独的jar放在类路径中,这两个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.

(我应该澄清这个答案适用于纯Java ,与任何特定的IDE或构建系统如何为项目生成jar无关.)

(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天全站免登陆