zoukankan      html  css  js  c++  java
  • 每个程序中只有一个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 }
  • 相关阅读:
    PsySH——PHP交互式控制台
    PHP通过ssh或socks5读取远程服务器的mysql数据库
    构建:vue项目配置后端接口服务信息
    module.exports用法
    PhpStorm连接服务器,开始自动上传功能
    JavaScript Array.some()方法用法
    vue-router query和params传参(接收参数),$router、$route的区别
    ES6箭头函数(Arrow Functions)
    工作中常用到的ES6语法
    VueJs2.0建议学习路线
  • 原文地址:https://www.cnblogs.com/meihao1989/p/3249033.html
Copyright © 2011-2022 走看看