我有一个程序,我需要通过将它转换为字符串来将Class对象存储到内存中。是否有可能将字符串转换回原始类,以便我可以使用该类变量?我正在用JAVA做这个。在Java中将字符串转换为字符串,反之亦然
例子:test.java
class hello{
public String h1;
public String h2;
}
public class test {
public static void main(String[] args)
{
hello h = new hello();
h.h1 = "hello";
h.h2 = "world";
String s = h.toString();
System.out.println("Print s : "+s);
// Now I need to convert String s into type hello so that
// I can do this:
// (hello)s.h1;
// (hello)s.h2;
}
}
注:这是不是一个功课,这是一个个人项目,我会如果有人能帮助感激不尽!
谢谢! Ivar
什么试图用h.toString()? – 2010-04-15 04:07:45
您不是将对象“投射”为String,而是调用返回对象的String描述(通常用于调试/日志记录)的方法(toString())。这不是一个可逆的操作 - 按Derek的建议看待序列化。 – 2010-04-15 04:10:07
请澄清:“需要将...存储到内存中”?你的意思是需要坚持它/将它保存在磁盘文件或数据库的某个地方吗? – 2010-04-15 05:31:18