每个程序中只有一个public类,主类?

 1 import java.io.*;
 2 
 3 public class GameSaverTest {
 4     public static void main(String[] args){
 5         //创建人物
 6         GameCharacter one=new GameCharacter(50,"Elf",new String[] {"bow","sword","dust"});
 7         GameCharacter two=new GameCharacter(200,"Troll",new String[] {"bare hands","big ax"});
 8         GameCharacter three=new GameCharacter(120,"Magician",new String[] {"spells","invisibility"});
 9         
10         
11         try{
12             ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream("Game.ser"));
13             os.writeObject(one);
14             os.writeObject(two);
15             os.writeObject(three);
16             os.close();
17         }catch(IOException ex){ex.printStackTrace();}
18         one=null;
19         two=null;
20         three=null;
21         
22         try{
23             ObjectInputStream is=new ObjectInputStream(new FileInputStream("Game.ser"));
24             GameCharacter oneRestore=(GameCharacter) is.readObject();
25             GameCharacter twoRestore=(GameCharacter) is.readObject();
26             GameCharacter threeRestore=(GameCharacter) is.readObject();
27             
28             System.out.println("One's type:"+oneRestore.getType());
29             System.out.println("Two's type:"+oneRestore.getType());
30             System.out.println("Three's type:"+oneRestore.getType());
31         }catch(Exception ex){
32             ex.printStackTrace();
33         }
34     }
35 
36 }

程序2:

 1 import java.io.*;
 2 
 3 
 4 public class GameCharacter implements Serializable{
 5     int power;
 6     String type;
 7     String[] weapons;
 8     
 9     public GameCharacter(int p,String t,String[] w){
10         power=p;
11         type=t;
12         weapons=w;
13     }
14     
15     public int getPower(){
16         return power;
17     }
18     public String getType(){
19         return type;
20     }
21     public String getWeapons(){
22         String weaponList="";
23         
24         for(int i=0;i<weapons.length;i++){
25             weaponList+=weapons[i]+" ";
26         }
27         return weaponList;
28     }
29 }

 

posted @ 2013-08-09 21:10  婷婷玉立的成长之家  阅读(506)  评论(0编辑  收藏  举报