Java裡關於複製與“冷藏”和“解凍”方法[00原創]

來源:互聯網
上載者:User
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;
    }

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.