forked from EdisonTalk/DesignPattern.Samples.CSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConcretePrototypeB.cs
38 lines (33 loc) · 1.04 KB
/
ConcretePrototypeB.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Manulife.ChengDu.DesignPattern.Prototype.PrepareAhead
{
public class ConcretePrototypeB : ICloneable
{
public int i = 0;
public string customAttr = "hello prototype";
public ConcretePrototype a = new ConcretePrototype();
public object Clone()
{
// 实现深复制-方式1:依次赋值和实例化
ConcretePrototypeB newObj = new ConcretePrototypeB();
newObj.a = new ConcretePrototype();
newObj.a.CustomAttr = this.a.CustomAttr;
newObj.i = this.i;
return newObj;
}
public new object MemberwiseClone()
{
// 实现浅复制
return base.MemberwiseClone();
}
public override string ToString()
{
string result = string.Format("I的值为{0},A为{1}", this.i.ToString(), this.a.CustomAttr);
return result;
}
}
}