把一份简历复制成多份浅复制实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Resume:ICloneable
{private string name;
private string sex;
private string age;
private string timearea;
private string company;
public Resume(string name)
{
this.name = name;
}
public void setinfo(string sex,string age)
{
this.sex = sex;
this.age = age;
}
public void setexperience(string timearea,string company)
{
this.timearea = timearea;
this.company = company;
}
public void display()
{
Console.WriteLine("{0} {1}{2}", name, sex, age);
Console.WriteLine("工作经历:{0}{1}", timearea, company);
}
public object Clone()
{
return (Object)this.MemberwiseClone();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
Resume a = new Resume("大鸟");
a.setinfo("男","29");
a.setexperience("1998-2000", "xx公司");
Resume b = (Resume)a.Clone();
b.setexperience("2001-2004", "xx公司");
Resume c = (Resume)a.Clone();
c.setinfo("男", "22");
a.display();
b.display();
c.display();
}
}
}
如果把工作经历设成一个类,就不能通过浅复制来实现,因为浅复制不能复制对对象的引用,复制的简历都指向同一地址。必须进行深复制
工作经历类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class WorkExperience:ICloneable
{
private string workdate;
public string WorkDate
{
get { return workdate; }
set { workdate = value; }
}
private string company;
public string Company
{
get { return company; }
set { company = value; }
}
public Object Clone()
{
return (Object)this.MemberwiseClone();
}
}
}
简历类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Resume:ICloneable
{private string name;
private string sex;
private string age;
WorkExperience work;
public Resume(string name)
{
this.name = name;
work = new WorkExperience();
}
private Resume(WorkExperience work)
{
this.work = (WorkExperience)work.Clone();
}
public void setinfo(string sex,string age)
{
this.sex = sex;
this.age = age;
}
public void setexperience(string timearea,string company)
{
work.WorkDate = timearea;
work.Company = company;
}
public void display()
{
Console.WriteLine("{0} {1}{2}", name, sex, age);
Console.WriteLine("工作经历:{0}{1}", work.WorkDate,work.Company);
}
public object Clone()
{
Resume obj = new Resume(this.work);
obj.name = this.name;
obj.sex = this.sex;
obj.age = this.age;
return obj;
}
}
}
客户端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
Resume a = new Resume("大鸟");
a.setinfo("男","29");
a.setexperience("1998-2000", "xx公司");
Resume b = (Resume)a.Clone();
b.setexperience("2001-2004", "xx公司");
Resume c = (Resume)a.Clone();
c.setinfo("男", "22");
a.display();
b.display();
c.display();
}
}
}