将String转换为类对象 [英] Convert String into a Class Object

查看:2645
本文介绍了将String转换为类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 toString()方法将类对象存储到字符串中。现在,我想将字符串转换为该类对象。

I am storing a class object into a string using toString() method. Now, I want to convert the string into that class object.

如何做到这一点?请帮我一下源代码。

How to do that? Please help me with source code.

推荐答案


我使用toString将一个类对象存储到一个字符串中() 方法。现在,我想将字符串转换为该类对象。

I am storing a class object into a string using toString() method. Now, I want to convert the string into that class object.

您的问题不明确。这可能意味着至少有两件不同的事情,其中​​之一就是......嗯......你的严重误解。

Your question is ambiguous. It could mean at least two different things, one of which is ... well ... a serious misconception on your part.

如果你这样做:

SomeClass object = ...
String s = object.toString();

然后答案是没有简单的方法来转 s 返回 SomeClass 的实例。即使 toString()方法给你一个时髦的SomeClass @ xxxxxxxx字符串,你也无法做到。 (该字符串不编码对象的状态,甚至不编码对象的引用.xxxxxxxx部分是对象的身份哈希码。它不是唯一的,不能神奇地转回参考到对象。)

then the answer is that there is no simple way to turn s back into an instance of SomeClass. You couldn't do it even if the toString() method gave you one of those funky "SomeClass@xxxxxxxx" strings. (That string does not encode the state of the object, or even a reference to the object. The xxxxxxxx part is the object's identity hashcode. It is not unique, and cannot be magically turned back into a reference to the object.)

你可以将 toString 的输出转回对象的唯一方法是:

The only way you could turn the output of toString back into an object would be to:


  • 编码 SomeClass.toString()方法,以便包含所有相关状态对于它生成的String中的对象,

  • 编译一个构造函数或工厂方法,它以 toString()方法。

  • code the SomeClass.toString() method so that included all relevant state for the object in the String it produced, and
  • code a constructor or factory method that explicitly parsed a String in the format produced by the toString() method.

这可能是一个糟糕的方法。当然,要为非平凡的课程做这件事需要做很多工作。

This is probably a bad approach. Certainly, it is a lot of work to do this for non-trivial classes.

如果你做了这样的事情:

If you did something like this:

SomeClass object = ...
Class c = object.getClass();
String cn = c.toString();

然后你可以获得相同的 Class 对象返回(即 c 中的那个),如下所示:

then you could get the same Class object back (i.e. the one that is in c) as follows:

Class c2 = Class.forName(cn);

这给你 Class 但是有没有神奇的方法来重建使用它的原始实例。 (显然,类的名称不包含对象的状态。)

This gives you the Class but there is no magic way to reconstruct the original instance using it. (Obviously, the name of the class does not contain the state of the object.)

如果您正在寻找一个如何序列化/反序列化任意对象,而不需要自己编写解码/解析方法,那么不应该使用 toString ()方法。以下是您可以使用的一些替代方法:

If you are looking for a way to serialize / deserialize an arbitrary object without going to the effort of coding the unparse / parse methods yourself, then you shouldn't be using toString() method at all. Here are some alternatives that you can use:


  • @Nishant答案中链接中描述的Java对象序列化API。

  • @ fatnjazzy的答案中描述的JSON序列化。

  • 像XStream这样的XML序列化库。

  • ORM映射。

  • The Java Object Serialization APIs as described in the links in @Nishant's answer.
  • JSON serialization as described in @fatnjazzy's answer.
  • An XML serialization library like XStream.
  • An ORM mapping.

这些方法中的每一种都有优点和缺点......我不会在这里讨论。

Each of these approaches has advantages and disadvantages ... which I won't go into here.

这篇关于将String转换为类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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