每个程序中只有一个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 }

 

转载于:https://www.cnblogs.com/meihao1989/p/3249033.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值