首页  韩国资源  酷站加油  我的展厅  设计名站  古典元素  推荐下载  设计欣赏  每周专访  招募精英  人才专区  网页教程  平面设计  编程开发  设计竞赛
当前位置:首页 > 编程开发 > JSP教程 > 正文
Google
关于Java克隆与"冷藏"和"解冻"方法
来源:68design.net 2007年08月30日 08:50 网友评论:0条 点击:

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;
    }

}

上一篇:构建高性能J2EE应用的十个技巧   下一篇:解决用户退出问题―JSP和Struts
收藏此页】【打印】【关闭
 相关文章  我要点评
·Javascript"篱式"条件判断
·介绍Java程序连接各种数据库的方法
·利用Jsvc把Java程序嵌入到Linux服务中去
·JSP+JavaScript打造二级级联下拉菜单
·Java Socket编程的一个秘密类
·用Java语言编写通过代理访问的应用程序
·Java开发最容易犯的21种错误
·Java中关于Hibernate对多表关联查询

免责声明:本站刊载此文不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。对本文有任何异议,请联络:68design#163.com
转载要求:作者及来源信息必需保留。转载之图片、文件,链接请不要盗链到本站,且不准打上各自站点的水印。



关于我们 | 在线反馈 | 广告报价 | 友情链接 | 联系我们 | 免责声明 | 在线投稿 | 网站地图
Copyright © 2003-2007 68design.net, All Rights Reserve 【找网页设计师,当然上网页设计师联盟】