动态铸造:
看起来是那么明显。如何将对象 myObjet 转换为 myObjet.getClass 的类型? 我尝试了 forName()、cast 和其他的组合...似乎都不起作用:s
问题是我有一个对象列表,我想让它们恢复到原来的类型(由 myObjet.getClass 给出) .getName())。
问候, Tim
是的,这就是 Java,而且它肯定没有错。让我解释一下这个问题。想象一个处理简单表单(例如 Squares 和 Circles)的程序,它们是主类 Form 的派生类。
因此,有一个返回表单的搜索函数,但您必须提供表单的类型和引用(例如数字):
Form myForm= searchForm(String formType, String formRef)
我知道它返回的表单是什么,因为我将这些参数提供给了搜索功能:例如正方形。返回的对象仍然是一个 Form,但我希望它是 Square 类,所以我需要转换它。它是兼容的,因为 Square 继承自 Form。
唯一的问题是“formType”是可变的,它可能确实是 Square,但也可能是“Circle”。这就是为什么我需要对返回对象进行通用转换。
如果我执行 myForm.getClass().getName(),我会得到“Square”。所以我需要将 myForm 转换为 myForm.getClass()。
你看到问题了吗?
It seems so obvious. How can I cast an object myObjet to the type of myObjet.getClass ?
I tried combinations of forName(), cast and others ... none seem to work :s
The problem is that I have a list of Objects and I would like to get them back to their original type (which is given by myObjet.getClass.getName()).
Regards,
Tim
Yes this is Java and it is certainly not wrong. Let me explain the problem. Picture a program that deals with simple forms like Squares and Circles that are derived classes from Form, the main class.
So there is a search function that returns a Form, but you have to provide the type and a reference of the form (a number, for example):
Form myForm= searchForm(String formType, String formRef)
I know what the Form it returns is going to be because I gave those arguments to the search function: a square, for instance. The returned object will still be a Form, but I want it to be of the Square class, so I need to cast it. It is compatible since Square inherits from Form.
The only problem is that "formType" is variable, it might indeed be Square, but it could also be "Circle". That is why I need a generic cast for the return object.
If I do a myForm.getClass().getName(), I get "Square". So I need to cast myForm to myForm.getClass().
Do you see the problem ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
转换不会改变对象本身。强制转换只会改变编译器允许您对对象执行的操作。
Java 提供了两种描述对象可以执行的操作的方法:类和接口。您投射到的内容必须是编译器在编译时知道的内容。否则,编译器无法阻止您做一些愚蠢的事情,例如尝试编写
myCircle.bark()
。因此,您有两个选择:如果这些类有一些共同点(实现的接口,或者它们都派生自的类),您可以尝试转换为该共同描述。但听起来这不是你想要的。相反,Java 强制您使用一系列
if/else
语句,这些语句尝试将您的对象强制转换为您可能想要执行某些操作的每个具体类,例如Square< /code>、
Circle
等。但是将对象强制转换为它自己的类是没有意义的,因为它不会在编译时向编译器提供有关该对象执行或执行哪些方法的任何信息没有。
Casting doesn't change the object itself. Casting only changes what the compiler allows you to do with the object.
Java provides two ways of describing what you can do with an object: classes and interfaces. The thing you cast to must be something the compiler knows about at compile time. Otherwise, the compiler can't prevent you from doing something silly, like trying to write
myCircle.bark()
.So you have two choices: if the classes have something in common (an implemented interface, or a class they all derive from) you could try casting to that common description. But it sounds like this is not what you want. Instead, Java forces you to do is to have a chain of
if/else
statements, which try to cast your object to each concrete class that you might want to do something with, likeSquare
,Circle
, etc.But casting an object to its own class doesn't make sense, because it doesn't give the compiler any information at compile time about what methods the object does or does not have.