如何转换类型的同一类,但从Java中的其他包? [英] How to cast a type of same class but out of an other package in Java?

查看:257
本文介绍了如何转换类型的同一类,但从Java中的其他包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

共有两个类

foo.bar.FileUploader barbar.foofoo .FileUploader

两者都是相同的,并扩展上传者

Both are identical and extend Uploader

在代码中,使用 foo.bar.FileUploader ,作为上传者类型。

Somewhere in the code the foo.bar.FileUploader is used and are given as a parameter to my function as an Uploader type.

现在我需要将它转回 FileUploader code> barbar.foofoo.FileUploader 。这会产生错误:

Now I need to cast it back to FileUploader but the barbar.foofoo.FileUploader. And this gives an error:


错误 - 未处理的错误
java.lang.ClassCastException
foo.bar.FileUploader 无法投放
barbar.foofoo.FileUploader

我不知道出了什么问题,我想这是因为包是不同的。在这种情况下:有没有办法仍然使用类并获得转换完成?

And I can't figure out what's going wrong, I suppose it's because the package is different. In that case: Is there a way to still use the class and get the cast done?

推荐答案

我刚刚通过导入barbar.foofoo.FileUploader来解决这个问题。现在它运行良好!

I just solved the problem by importing the the barbar.foofoo.FileUploader. Now it runs fine!

这篇关于如何转换类型的同一类,但从Java中的其他包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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