可以使用两个具有相同名称和相同包的java类吗? [英] Possible to use two java classes with same name and same package?

查看:865
本文介绍了可以使用两个具有相同名称和相同包的java类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在java中导入和使用两个具有相同名称和包的不同类?

Is it possible to import and use two different classes with the same name and package in java?

例如,假设我有两个名为com的类。 foo.Bar略有不同。我希望能够同时使用两者,但我有一个限制(因为愚蠢的反光废话)迫使我保持​​名称和包装相同。

For example, let's say I have two classes named "com.foo.Bar" that are slightly different. I'd like to be able to use both, but I have a restriction (because of stupid reflective crap) that forces me to keep the names and packages the same.

是否有一些java的功能允许我导入并隔离这些类中的每一个?

Is there some feature of java that would allow me to import and isolate each of these classes?

详细说明,我改变了我的avro模式,他们应该没有从来没有被改变过(哎呀!)现在我想回去把我新架构无法读取的旧avro文件改成我的新架构可以读取的文件。 Avro似乎强迫您使用特定的类和包名称来加载文件。

To elaborate, I changed my avro schemas in ways that they shouldn't have ever been changed (oops!) and now I'd like to go back and change the old avro files that can't be read with my new schema into files that can be read by my new schema. Avro seems to force you to use a specific class and package name to load the files.

推荐答案

是的。您需要实现自己的类加载器并玩一些游戏才能在运行时访问它们。

Yes there is. You would need to implement your own Classloader and play some games to be able to access both during runtime.

我确信这是可能的,因为我碰到了一个非常难以调试的问题,有人在他们的产品中有一个奇怪的类加载器,搞乱了加载库并从库的2个不同版本提供相同文件的2个不同版本。

I'm sure this is possible, because I ran into a very hard to debug issue where someone had a weird Classloader in their product that was messing up loading libraries and providing 2 different versions of the same file from 2 different versions of the library.

但是,这听起来像一个令人难以置信的坏主意。我会回去找一个解决问题的不同方法。从长远来看,这只会给你带来痛苦。哎呀,它可能已经是,你在调查类加载器时。

However, this sounds like an INCREDIBLY bad idea. I'd go back and find a different way of fixing your issue. This will only bring you heartache in the long run. Heck, it probably already is, as you investigate class loaders.

编辑:具体来说,你不能导入两者。但是你可以在运行时访问它们。

To be specific, you cannot "import" both. But you can access both at runtime.

这篇关于可以使用两个具有相同名称和相同包的java类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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