我有一個程序,我需要通過將它轉換爲字符串來將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