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

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

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

Create a clone using the Object.MemberwiseClone method because the Employee class contains only string and value types

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 MainClass
{
    public static void Main()
    {
        Employee em = new Employee("A", "AA", 4);
        Employee cloneEmployee = (Employee)em.Clone();
        Console.WriteLine("Original Employee:");
        Console.WriteLine(em);
        Console.WriteLine("Clone Employee:");
        Console.WriteLine(cloneEmployee);
        Console.WriteLine("*** Make a change to original employee ***");
        em.Name = "L";
        em.Title = "M";
        em.Age = 4;
        Console.WriteLine("Original Employee:");
        Console.WriteLine(em);
        Console.WriteLine("Clone Employee:");
        Console.WriteLine(cloneEmployee);
    }
}
Original Employee:
A (AA) - Age 4
Clone Employee:
A (AA) - Age 4
*** Make a change to original employee ***
Original Employee:
L (M) - Age 4
Clone Employee:
A (AA) - Age 4

Perform a memberwise clone of myEmployee using the Employee.Copy() method

using System;
public class Employee
{
  public string firstName;
  public string lastName;
  public Employee(string firstName, string lastName)
  {
    this.firstName = firstName;
    this.lastName = lastName;
  }
  public void Display()
  {
    Console.WriteLine("firstName = " + firstName);
    Console.WriteLine("lastName = " + lastName);
  }
  public static Employee Copy(Employee Employee)
  {
    return (Employee) Employee.MemberwiseClone();
  }
}

class MainClass
{
  public static void Main()
  {
    Console.WriteLine("Creating Employee objects");
    Employee myEmployee = new Employee("A", "M");
    Employee myOtherEmployee = new Employee("B", "N");
    Console.WriteLine("myEmployee details:");
    myEmployee.Display();
    Console.WriteLine("myOtherEmployee details:");
    myOtherEmployee.Display();

    Console.WriteLine("Performing a memberwise clone of myEmployee to myOldEmployee");
    Employee myOldEmployee = Employee.Copy(myEmployee);
    Console.WriteLine("myOldEmployee details:");
    myOldEmployee.Display();
  }
}
Creating Employee objects
myEmployee details:
firstName = A
lastName = M
myOtherEmployee details:
firstName = B
lastName = N
Performing a memberwise clone of myEmployee to myOldEmployee
myOldEmployee details:
firstName = A
lastName = M

Use MemberwiseClone method to clone object

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