国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Java關于克隆與“冷藏”和“解凍”方法

2019-11-17 05:54:58
字體:
來源:轉載
供稿:網友

import java.awt.Point;
import java.io.IOException;

import com.sun.corba.se.impl.io.OptionalDataException;

/**
 * 克隆測試<br>
 * 以方形類為例,比較了深克隆(deep clone)與淺克隆(shallow clone)的異同
 *
 * @see #clone()
 * @author 88250
 * @version 1.0.0, 2007-8-26
 */
public class CloneTester
{
    PRivate Square square = new Square();

    private Square cpySquare = null;

    /**
     * 淺克隆操作
     */
    public void shallowClone()
    {
    square.setSideLength(2);
    square.setLocation(new Point(2, 5));
    // 淺克隆
    cpySquare = (Square) square.clone();

    }

    /**
     * 深克隆操作
     */
    public void deepClone()
    {
    square.setSideLength(3);
    square.setLocation(new Point(1, 3));
    // 深克隆
    try
    {
        cpySquare = (Square) square.deepClone();
    }
    catch (OptionalDataException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    catch (ClassNotFoundException e)
    {
        e.printStackTrace();
    }
    }

    /**
     * 克隆結果輸出
     */
    public void cloneDisplay()
    {

    System.out.println("原始方形長度:" + square.getSideLength());
    System.out.println("克隆方形長度:" + cpySquare.getSideLength());

    System.out.println("原始方形==克隆方形?" + (square == cpySquare));

    System.out.println("原始方形的位置==克隆方形的位置?"
        + (square.getLocation() == cpySquare.getLocation()));
    }

    public static void main(String[] args)
    {
    CloneTester sm = new CloneTester();
    sm.shallowClone();
    sm.cloneDisplay();

    sm.deepClone();
    sm.cloneDisplay();
    }
}


import java.awt.Point;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import com.sun.corba.se.impl.io.OptionalDataException;

/**
 * 正方形
 *
 * @author 88250
 * @version 1.0.0, 2007-8-26
 */
public class Square implements Cloneable, Serializable
{
    private Point location = new Point(0, 0);

    private float sideLength = 1F;

    @Override
    public Object clone()
    {
    Square tmp = null;
    try
    {
        tmp = (Square) super.clone();
    }
    catch (CloneNotSupportedException cnse)
    {
        cnse.printStackTrace();
    }
    finally
    {
        return tmp;
    }
    }
   
    /**
     * 深克隆方法
     * @return
     */
    public Object deepClone()
    throws IOException, OptionalDataException, ClassNotFoundException
    {
    // 首先將對象寫到流里
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    ObjectOutputStream oo = new ObjectOutputStream(bo);
    oo.writeObject(this);
   
    // 然后將對象從流里讀出來
    ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
    ObjectInputStream oi = new ObjectInputStream(bi);
   
    return (oi.readObject());
    }

    /**
     * @return the location
     */
    public Point getLocation()
    {
        return location;
    }

    /**
     * @param location the location to set
     */
    public void setLocation(Point location)
    {
        this.location = location;
    }

    /**
     * @return the sideLength
     */
    public float getSideLength()
    {
        return sideLength;
    }

    /**
     * @param sideLength the sideLength to set

     */
    public void setSideLength(float sideLength)
    {
        this.sideLength = sideLength;
    }

}



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 锡林郭勒盟| 张家口市| 正蓝旗| 海淀区| 融水| 永德县| 延长县| 天水市| 东台市| 红河县| 东山县| 忻州市| 敖汉旗| 正阳县| 都昌县| 页游| 茌平县| 财经| 济宁市| 石家庄市| 永顺县| 黄骅市| 方正县| 张家界市| 龙江县| SHOW| 苏尼特左旗| 个旧市| 平舆县| 绥化市| 会东县| 永兴县| 无为县| 汉沽区| 永州市| 德钦县| 淳化县| 德钦县| 定结县| 南川市| 桐城市|