我正在尝试创建一种在Java中保存/加载对象的方法,我有类Item:
public class Item implements Serializable{
//Variables
private String name, desc;
private int price;
//Constructors
public Item(){
this.name=this.desc="NA";
}
public Item(String name,String desc,int price){
this.name=name;
this.desc=desc;
this.price=price;
}
public Item(String name){
load(name);
}
public Item(Item i){
this.name=i.name;
this.desc=i.desc;
this.price=i.price;
}
//Getters and Setters
public void setName(String name) {
this.name = name;
}
public void setPrice(int price) {
this.price = price;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getName() {
return name;
}
public String getDesc() {
return desc;
}
public int getPrice() {
return price;
}
public void save(){
try{
String path = System.getProperty("user.home") + "/JavaGame/Item/";
new File(path).mkdirs();
path+=this.name;
path+=".dat";
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(path));
outputStream.writeObject(this);
outputStream.flush();
outputStream.close();
}
catch(Exception e){
System.out.println("Error saving: "+e.getMessage());
}
}
public void load(String name){
try {
String path = System.getProperty("user.home") + "/JavaGame/Item/";
path+=name;
path+=".dat";
ObjectInputStream in = new ObjectInputStream(new FileInputStream(path));
Item i = (Item) in.readObject();
this.name=name;
this.desc=i.desc;
this.price=i.price;
in.close();
}
catch(Exception e){
System.out.println("Error loading: "+e.getMessage());
}
}
public boolean equals(Object o){
if(o==null||o.getClass()!=this.getClass())return false;
Item i = (Item) o;
return i.name.equals(this.name);
}
public String toString(){
return new StringBuilder().append(this.name).append("\n").append(this.desc).toString();
}
public Item clone(){
return new Item(this);
}
}
我想创建项目类型,例如可用和装甲(例如)。所以我将创建一个名为ArmorPiece的新类,它只有一个防御属性。 我们假设代码是:
public class ArmorPiece extends Item{
private int defense;
public ArmrPiece(String name, int defense){
setName(name);
this.defense = defense;
}
public String toString(){
return "It's an armor piece";
}
}
在这个项目中,我将所有项目信息保存在文件夹/ JavaGame / Item /中,并且我想将所有项目保存在同一文件夹中,当我运行如下所示的测试时,它进入了else括号,你知道吗?使“物品”变成“ArmorPiece”?
public static void main(String args[]){
new ArmorPiece("boots",10).save();
Item m = new Item("boots");//It will load the boots info into an Item
if(m.getClass().getName().equals("ArmorPiece")){//It should enter the if since it is an ArmorPiece
ArmorPiece ap=(ArmorPiece) m;
System.out.println(ap);
}
else{//Enters this brackets, but it shouldn't
System.out.println(m);
}
}
答案 0 :(得分:0)
这是因为你创建了Item
类。您无法在创建后更改对象类。我建议您将load
方法设为静态并从中返回i
。
答案 1 :(得分:-1)
它可能是有用的,但我认为你应该使用一种方法一次加载所有内容(例如在HashMap中),然后在那里查找它,你甚至可以在打开程序时加载Map。例如:
public class AllItems{
Map<String,Item> allItems;
public AllItems(){
allItems = new HashMap<String,Item>();
//Load all items here(You can save them all in one file)
}
public Item getItem(String name){
return allItems.get(name);//You need to make this always work, so you should check if the item exists first.
}
}
这只是一个例子,但您可以尝试这样做。