Csharp/CSharp Tutorial/Class/ICloneable — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

Cloneable Point

using System;
using System.Collections.Generic;
using System.Text;

public class Point : ICloneable {
    public int x, y;
    public PointDescription desc = new PointDescription();
    public Point() { }
    public Point(int x, int y) {
        this.x = x; this.y = y;
    }
    public Point(int x, int y, string petname) {
        this.x = x;
        this.y = y;
        desc.petName = petname;
    }
    public object Clone() {
        Point newPoint = (Point)this.MemberwiseClone();
        PointDescription currentDesc = new PointDescription();
        currentDesc.petName = this.desc.petName;
        newPoint.desc = currentDesc;
        return newPoint;
    }
    public override string ToString() {
        return string.Format("X = {0}; Y = {1}; Name = {2};\nID = {3}\n",x, y, desc.petName, desc.pointID);
    }
}
public class PointDescription {
    public string petName;
    public Guid pointID;
    public PointDescription() {
        this.petName = "No-name";
        pointID = Guid.NewGuid();
    }
}
class Program {
    static void Main(string[] args) {
        Point p1 = new Point(50, 50);
        Point p2 = p1;
        p2.x = 0;
        Console.WriteLine(p1);
        Console.WriteLine(p2);
        Point p3 = new Point(100, 100, "Jane");
        Point p4 = (Point)p3.Clone();
        Console.WriteLine("p3: {0}", p3);
        Console.WriteLine("p4: {0}", p4);
        p4.desc.petName = "Mr. X";
        p4.x = 9;
        Console.WriteLine("p3: {0}", p3);
        Console.WriteLine("p4: {0}", p4);
    }
}

Clone a list of cloneable objects

using System;
using System.Text;
using System.Collections.Generic;
public class Employee : ICloneable
{
    public string Name;
    public string Title;
    public int Age;
    public Employee(string name, string title, int age)
    {
        Name = name;
        Title = title;
        Age = age;
    }
    public object Clone()
    {
        return MemberwiseClone();
    }
    public override string ToString()
    {
        return string.Format("{0} ({1}) - Age {2}", Name, Title, Age);
    }
}
public class EmployeeList : ICloneable
{
    public List<Employee> EmployeeListMembers =  new List<Employee>();
    public EmployeeList()
    {
    }
    private EmployeeList(List<Employee> members)
    {
        foreach (Employee e in members)
        {
            EmployeeListMembers.Add((Employee)e.Clone());
        }
    }
    public void AddMember(Employee member)
    {
        EmployeeListMembers.Add(member);
    }
    public override string ToString()
    {
        StringBuilder str = new StringBuilder();
        foreach (Employee e in EmployeeListMembers)
        {
            str.AppendFormat("  {0}\r\n", e);
        }
        return str.ToString();
    }
    public object Clone()
    {
        return new EmployeeList(this.EmployeeListMembers);
    }
}
public class MainClass
{
    public static void Main()
    {
        EmployeeList team = new EmployeeList();
        team.AddMember(new Employee("A", "AA", 4));
        team.AddMember(new Employee("B", "BB", 8));
        team.AddMember(new Employee("C", "CC", 8));
        EmployeeList clone = (EmployeeList)team.Clone();
        Console.WriteLine("Original EmployeeList:");
        Console.WriteLine(team);
        Console.WriteLine("Clone EmployeeList:");
        Console.WriteLine(clone);
        Console.WriteLine("*** Make a change to original team ***");
        team.EmployeeListMembers[0].Name = "L";
        team.EmployeeListMembers[0].Title = "M";
        team.EmployeeListMembers[0].Age = 4;
        Console.WriteLine("Original EmployeeList:");
        Console.WriteLine(team);
        Console.WriteLine("Clone EmployeeList:");
        Console.WriteLine(clone);
    }
}
Original EmployeeList:
  A (AA) - Age 4
  B (BB) - Age 8
  C (CC) - Age 8
Clone EmployeeList:
  A (AA) - Age 4
  B (BB) - Age 8
  C (CC) - Age 8
*** Make a change to original team ***
Original EmployeeList:
  L (M) - Age 4
  B (BB) - Age 8
  C (CC) - Age 8
Clone EmployeeList:
  A (AA) - Age 4
  B (BB) - Age 8
  C (CC) - Age 8

Implement ICloneable interface

using System;
class MyValue
{
    public MyValue(int count)
    {
        this.count = count;
    }
    public int count;
}
class MyObject: ICloneable
{
    public MyObject(int count)
    {
        this.contained = new MyValue(count);
    }
    public object Clone()
    {
        Console.WriteLine("Clone");
        return(new MyObject(this.contained.count));
    }
    public MyValue contained;
}
class MainClass
{
    public static void Main()
    {
        MyObject my = new MyObject(33);
        MyObject myClone = (MyObject) my.Clone();
        Console.WriteLine("Values: {0} {1}", my.contained.count, myClone.contained.count);
        myClone.contained.count = 15;
        Console.WriteLine("Values: {0} {1}", my.contained.count, myClone.contained.count);
    }
}
Clone
Values: 33 33
Values: 33 15