ICloneable接口

  如果想使自己的自定义类型支持向调用方返回自身同样副本的能力,需要实现标准ICloneable接口。

 namespace System
{ public interface ICloneable
{
object Clone();
}
}

浅拷贝

  System.Object定义了一个名为MemberwiseClone()的成员。这个方法用来获取当前对象的一份浅拷贝。

例:

Point.cs
 namespace CloneablePoint
{
// The Point now supports "clone-ability."
public class Point : ICloneable
{
public int X { get; set; }
public int Y { get; set; }
public PointDescription desc = new PointDescription(); public Point( int xPos, int yPos, string petName )
{
X = xPos; Y = yPos;
desc.PetName = petName;
}
public Point( int xPos, int yPos )
{
X = xPos; Y = yPos;
}
public Point() { } // Override Object.ToString().
public override string ToString()
{
return string.Format("X = {0}; Y = {1}; Name = {2};\nID = {3}\n",
X, Y, desc.PetName, desc.PointID);
} // Return a copy of the current object.
// Now we need to adjust for the PointDescription member.
public object Clone()
{
// 这个复制每个Ponit字段成员
Point newPoint = (Point)this.MemberwiseClone();
return newPoint;
}
}
}
PointDescription.cs
 namespace CloneablePoint
{
// This class describes a point.
public class PointDescription
{
public string PetName { get; set; }
public Guid PointID { get; set; } public PointDescription()
{
PetName = "No-name";
PointID = Guid.NewGuid();
}
}
}
Program.cs
 namespace CloneablePoint
{
class Program
{
static void Main( string[] args )
{
Console.WriteLine("***** Fun with Object Cloning *****\n");
Console.WriteLine("Cloned p3 and stored new Point in p4");
Point p3 = new Point(, , "Jane");
Point p4 = (Point)p3.Clone(); Console.WriteLine("Before modification:");
Console.WriteLine("p3: {0}", p3);
Console.WriteLine("p4: {0}", p4);
p4.desc.PetName = "My new Point";
p4.X = ; Console.WriteLine("\nChanged p4.desc.petName and p4.X");
Console.WriteLine("After modification:");
Console.WriteLine("p3: {0}", p3);
Console.WriteLine("p4: {0}", p4);
Console.ReadLine();
}
}
}

  请注意,如果Ponint包含任何引用类型成员变量,MemberwiseClone()将这些引用复制到对象中(即浅复制)。如果想要支持真正的深复制,需要在克隆过程中创建任何引用类型变量的新实力。

深拷贝

Point.cs

namespace CloneablePoint
{
public class Point : ICloneable
{
public int X { get; set; }
public int Y { get; set; }
public PointDescription desc = new PointDescription(); public Point( int xPos, int yPos, string petName )
{
X = xPos; Y = yPos;
desc.PetName = petName;
}
public Point( int xPos, int yPos )
{
X = xPos; Y = yPos;
}
public Point() { } public override string ToString()
{
return string.Format("X = {0}; Y = {1}; Name = {2};\nID = {3}\n",
X, Y, desc.PetName, desc.PointID);
} public object Clone()
{
Point newPoint = (Point)this.MemberwiseClone(); PointDescription currentDesc = new PointDescription();
currentDesc.PetName = this.desc.PetName;
newPoint.desc = currentDesc;
return newPoint;
}
}
}

PointDescription.cs

namespace CloneablePoint
{
public class PointDescription
{
public string PetName { get; set; }
public Guid PointID { get; set; } public PointDescription()
{
PetName = "No-name";
PointID = Guid.NewGuid();
}
}
}

Program.cs

namespace CloneablePoint
{
class Program
{
static void Main( string[] args )
{
Console.WriteLine("***** Fun with Object Cloning *****\n");
Console.WriteLine("Cloned p3 and stored new Point in p4");
Point p3 = new Point(, , "Jane");
Point p4 = (Point)p3.Clone(); Console.WriteLine("Before modification:");
Console.WriteLine("p3: {0}", p3);
Console.WriteLine("p4: {0}", p4);
p4.desc.PetName = "My new Point";
p4.X = ; Console.WriteLine("\nChanged p4.desc.petName and p4.X");
Console.WriteLine("After modification:");
Console.WriteLine("p3: {0}", p3);
Console.WriteLine("p4: {0}", p4);
Console.ReadLine();
}
}
}

构建可克隆的对象(ICloneable)的更多相关文章

  1. ES5-ES6-ES7_字符串与JOSN格式的数据相互转换以及深度克隆新对象

    这篇文章主要来讲HTML5中的新方法:parse()把字符串转换成josn格式的数据和stringify()把josn格式的数据转换成字符串 eval()方法的回顾 eval()方法可以将任何字符串解 ...

  2. JavaScript面向对象编程(10)高速构建继承关系之对象拷贝

    前面的样例我们是通过构造器创建对象.而且希望该对象继承来自另外一个构造器的对象 我们也能够直接面向一个对象来达成继承的目的.使用下属步骤: 1.拷贝一个对象 2.给新对象加入属性 /** * 通过拷贝 ...

  3. 利用BeanUtils.copyProperties 克隆出新对象,避免对象重复问题

    1.经常用jQuery获取标签里面值val(),或者html(),text()等等,有次想把获取标签的全部html元素包括自己也用来操作,查询了半天发现$("#lefttr1"). ...

  4. XAF ORMDataModel构建的基础资料对象无法被调用显示的解决办法

    修正,其实只要在基础资料类中加入[XafDefaultProperty("名称")]标签即可. namespace NEO.ERP.Module.BusinessObjects.B ...

  5. ES6 克隆对象 浅克隆:只能克隆原始对象自身的值,不能克隆它继承的值

    https://www.cnblogs.com/xbblogs/p/8954165.html return JSON.parse(JSON.stringify(origin)) 最早由Barbara ...

  6. 深度克隆(对象、数组)--------百度IFE前端task2

    var srcObj = { a: 1, b: { b1: ["hello", "hi"], b2: "JavaScript" }}; co ...

  7. 在JAVASCRIPT中构建一个复杂的对象,并用JSON进行转换

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 精通C#(第6版)

    <精通C#(第6版)> 基本信息 原书名:Pro C# 5.0 and the .NET 4.5 framework,sixth edition 作者: (美)Andrew Troelse ...

  9. .NET 接口

    接口      接口是一组抽象成员的集合,表示某个类或结构可以选择去实现的行为,描述的是可属于任何类或结构的一组相关功能.接口方法的实现是在实现接口的类中完成的,实现接口的类可以显式实现该接口的成员, ...

随机推荐

  1. [转贴]JAVA :CXF 简介

    Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXF.CXF 继 ...

  2. Android使用SeekBar时动态显示进度且随SeekBar一起移动

    最近有做一个android项目,里面有使用到在播放视频时可以跳播,同时动态显示播放时间.类似于下图 的效果,我只是抽取其中的一部分做展示,刚接到这个事时也是在网上一通找,最后没找到!而且还碰到有些朋友 ...

  3. unicode转中文

    <pre name="code" class="html">[root@dr-mysql01 ~]# cat a1.pl my $str=" ...

  4. WPF WebBroswer可以用到的接口

    http://pinvoke.net/default.aspx/Interfaces.DWebBrowserEvents2 [ComImport, SuppressUnmanagedCodeSecur ...

  5. POJ -- 2436

    Disease Management Description Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D) is run ...

  6. poj 2586 Y2K Accounting Bug

    http://poj.org/problem?id=2586 大意是一个公司在12个月中,或固定盈余s,或固定亏损d. 但记不得哪些月盈余,哪些月亏损,只能记得连续5个月的代数和总是亏损(<0为 ...

  7. 推荐一款自己的软件作品[豆约翰博客备份专家],新浪博客,QQ空间,CSDN,cnblogs博客备份,导出CHM,PDF(转载)

    推荐一款自己的软件作品[豆约翰博客备份专 豆约翰博客备份专家是完全免费,功能强大的博客备份工具,博客电子书(PDF,CHM和TXT)生成工具,博文离线浏览工具,软件界面美观大方,支持多个主流博客网站( ...

  8. PHP 获取时间的各种处理方式!

    今天写下php中,如何通过各种方法 获取当前系统时间.时间戳,并备注各种格式的含义,可灵活变通.1.获取当前时间方法date()很简单,这就是获取时间的方法,格式为:date($format, $ti ...

  9. 【转载】zookeeper 分布式锁 实现

      agapple 基于zookeeper的分布式lock实现 博客分类: opensource java distributed   背景 继续上一篇文章:http://agapple.iteye. ...

  10. 一步一步写一个简单通用的makefile(一)

    经常会用写一些小的程序有的是作为测试,但是每次都需要写一些简单的GCC 命令,有的时候移植一些项目中的部分代码到小程序里面进行测试,这个时候GCC 命令并不好些,如果写啦一个比较常用的makefile ...