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

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

问题描述

我正在使用 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() 方法进行编码,以便在它生成的字符串中包含对象的所有相关状态,以及
  • 编写一个构造函数或工厂方法,以 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 映射.

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

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

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

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