请问大家庭 java.io.Serializable 的作用是什么?举个例子,散分

解决方案 »

  1.   

    Serializable用在两个方面:
    1。网络传输
    2。数据库持久比如你的某个object需要持久到数据库中,就得实现这个接口。
      

  2.   

    正好在写着,呵呵,给个例子啊import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;/**
     * @author Yan Chenyang
     *
     * TODO 要更改此生成的类型注释的模板,请转至
     * 窗口 - 首选项 - Java - 代码样式 - 代码模板
     */
    public class  JFunction implements Serializable{ private double[][] bounds;
    private int vnum;
    private double result;
    private String funcname;

    public JFunction(){
    super();
    this.bounds=null;
    this.vnum=0;
    this.result=0.0;
    this.funcname=null;
    }

    public JFunction(double[][] bounds){
    super();
    this.bounds=bounds;
    this.vnum=bounds[0].length;
    this.result=0.0;
    this.funcname="Function 1";
    }

    public double Func(double[] var){

    result=0.0;
    for(int i=0;i<var.length;i++)
    result+=var[i];
    return result;
    }


    public static JFunction deserialize(ObjectInputStream oin) 
    throws Exception{

    JFunction f=(JFunction)oin.readObject();
            return f; }

    public void serialize(ObjectOutputStream oout) 
    throws Exception{
    oout.writeObject(this);
    }

    public String toString(){
    return funcname ;

    }
    /**
     * @return 返回 funcname。
     */
    public String getFuncname() {
    return funcname;
    }
    /**
     * @param funcname 要设置的 funcname。
     */
    public void setFuncname(String funcname) {
    this.funcname = funcname;
    }
    }public class JFunctionTest0 { public static void main(String[] args){

    double[][] bounds={{-2.048,-2.048},{2.048,2.048}};
    JFunction function=new JFunction(bounds);

    Interpreter interpreter=new Interpreter();

    try {

    double[] x={2.15,1.00};
    interpreter.set("var",x);
    interpreter.source("c:/x.bsh");
    System.out.println(interpreter.get("ret"));

    } catch (EvalError e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }


    FileInputStream in=null;
            FileOutputStream out=null;
            ObjectInputStream oin=null;
            ObjectOutputStream oout=null;
            
            try{
             out = new FileOutputStream("function1.func");
             oout = new ObjectOutputStream(out);
             function.serialize(oout);//序列化
             oout.close();
             oout=null;         in = new FileInputStream("function1.func");
             oin = new ObjectInputStream(in);
             JFunction tfunction =JFunction.deserialize(oin);//反序列化
            
             double[] var={2.0,5.0};
            
     //        System.out.println(tfunction.Func(var));
     //        System.out.println(tfunction);//打印结果
     //       
            
            }catch(Exception ex){
             ex.printStackTrace();
            }finally{
             try {
                    if (in != null) {
                        in.close();
                    }
                    if (oin != null) {
                        oin.close();
                    }
                    if (out != null) {
                        out.close();
                    }
                    if (oout != null) {                    oout.close();
                    }
                } catch (IOException ex1) {
                    ex1.printStackTrace();
                }
            }       
    }
    }
      

  3.   

    封装了一个函数类,implemnets了Serializable接口,在JFunctionTest0类中进行了序列化和发序列化,把这个类写入文件,又从文件中恢复