Cloneable Point : ICloneable « Class « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Class » ICloneable 
7.51.3.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(5050);
        Point p2 = p1;
        p2.x = 0;

        Console.WriteLine(p1);
        Console.WriteLine(p2);

        Point p3 = new Point(100100"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);
    }
}
7.51.ICloneable
7.51.1.Implement ICloneable interface
7.51.2.Clone a list of cloneable objects
7.51.3.Cloneable Point
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.