我想將String
轉換爲對象。我查找了相關主題,但找不到答案。我的代碼是:java String to a object
amountShip = 5;
String name = ReadConsole.nextString(); //-->variabe of the object "Player"
gamers[i] = Player.String2Object(name, 0);
Player String2Object(String name, int amountShip) {
Object o = name;
Player temp = (Player)o;
temp = new Player(name, amountShip);
return temp;
}
class Player {
Player (String name, int amountShip) {
name = name;
ships = amountShip;
}
Player pl = new Player(String name, int amountShip);
}
它說
java.lang.String中不能轉換到Gameobjects.Player
我的目的是創建2個不同的實例名球員的對象動態。如果在「3」例如有人類型,我想提前
'Object o = name; Player temp =(Player)o;'你認爲這樣做了什麼?如果你回答它,那麼你解決了你的問題。 –
Java很棒,但它不能做魔術!不過,你應該真正修復格式並提供更多信息(請參閱http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug - 我的應用程序,錯誤) – isnot2bad