
设计模式 - 7) 原型模式
发布日期:2021-05-13 19:47:06
浏览次数:26
分类:博客文章
本文共 2735 字,大约阅读时间需要 9 分钟。
������������������������������������������������������������������ Clone ������������������������������������������������������������������
1. ������
////// ���������������/// public abstract class Prototype{ public abstract Prototype Clone();}public class Companey{ public string Name { get; set; } public string Place { get; set; }}////// ���������������/// public class Resume : Prototype{ public string Name { get; set; } public Companey Com { get; set; } public object Clone() { return (object)this.MemberwiseClone(); } public void Show() { Console.WriteLine( string.Format("{0} ������������{1}", name, Com.Name + Com.Place)); }}Resume a = new Resume() { Name = "Mao", Com = new Companey() { Name = "Gxx", Place = "2819" } };Resume b = (Resume)a.Clone();Resume c = (Resume)a.Clone();a.Show();b.Show();c.Show();a.Name = "a";b.Name = "b";a.Show();b.Show();c.Show();
b���c ���������a��������������� a ��� Name ��� b���c ��������������������������� b ��� Name ������������������������������
Mao ���������Gxx2819Mao ���������Gxx2819Mao ���������Gxx2819a ���������Gxx2819b ���������Gxx2819Mao ���������Gxx2819
2. C# ��������� ICloneable
C# ������������ ICloneable ������������������������ Prototype ���������������������������
public class Resume : ICloneable { }
3. ���������������������
������ a ��� Name ������������������ string��������������������������� a ��� Name ��������� b���c ������������������������������������������ Com ������������������������������������
Resume a = new Resume() { Name = "Mao", Com = new Companey() { Name = "Gxx", Place = "2819" } };Resume b = (Resume)a.Clone();Resume c = (Resume)a.Clone();a.Show();b.Show();c.Show();a.Name = "a";a.Com.Place = "2866";b.Name = "b";b.Com.Place = "2828";a.Show();b.Show();c.Show();
���������
Mao ���������Gxx2819Mao ���������Gxx2819Mao ���������Gxx2819a ���������Gxx2828b ���������Gxx2828Mao ���������Gxx2828
������������������������ a ������������������������������������������������������������������������������������������������������������������.
4. ���������������
public class Companey : ICloneable{ public string Name { get; set; } public string Place { get; set; } public object Clone() { return (object)this.MemberwiseClone(); }}public class Resume : ICloneable{ ... public Resume() { } public Resume(Companey com) { this.Com = (Companey)com.Clone(); } public object Clone() { Resume obj = new Resume(this.Com); obj.Name = this.Name; obj.Age = this.Age; obj.Sex = this.Sex; return obj; }}
���������
Mao ���������Gxx2819Mao ���������Gxx2819Mao ���������Gxx2819a ���������Gxx2866b ���������Gxx2828Mao ���������Gxx2819
发表评论
最新留言
表示我来过!
[***.240.166.169]2025年04月11日 10时43分58秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
spring-boot-2.0.3之redis缓存实现,不是你想的那样哦!
2021-05-09
httprunner学习23-加解密
2021-05-09
有道云笔记 同步到我的博客园
2021-05-09
李笑来必读书籍整理
2021-05-09
http头部 Expect
2021-05-09
Hadoop(十六)之使用Combiner优化MapReduce
2021-05-09
《机器学习Python实现_10_06_集成学习_boosting_gbdt分类实现》
2021-05-09
CoreCLR源码探索(八) JIT的工作原理(详解篇)
2021-05-09
IOS开发Swift笔记16-错误处理
2021-05-10
flume使用中的一些常见错误解决办法 (地址已经使用)
2021-05-10
andriod 开发错误记录
2021-05-10
C语言编译错误列表
2021-05-10
看明白这两种情况,才敢说自己懂跨链! | 喵懂区块链24期
2021-05-10
张一鸣:创业7年,我经历的5件事
2021-05-10
git拉取远程指定分支代码
2021-05-10
《web安全入门》(四)前端开发基础Javascript
2021-05-10
python中列表 元组 字典 集合的区别
2021-05-10
python struct 官方文档
2021-05-10