Office中国论坛/Access中国论坛

标题: C#深复制的测试 [打印本页]

作者: tianping    时间: 2014-4-3 22:51
标题: C#深复制的测试
本帖最后由 tianping 于 2014-4-3 22:57 编辑
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;

  6. namespace testConsole
  7. {
  8.     enum work
  9.     {
  10.         dancer,        
  11.         teacher
  12.     }
  13.     class Man
  14.     {
  15.         public string Name { get; set; }
  16.        public work W{get;set;}
  17.        public object Clone()
  18.        {
  19.            Man newMan = new Man();
  20.            newMan.W = this.W;
  21.            newMan.Name = this.Name;
  22.            return newMan;

  23.        }
  24.     }
  25.     class A:ICloneable
  26.     {
  27.         public string S { get; set; }
  28.         public Man P=new Man();
  29.         public A MakeCopy()
  30.         {
  31.             return (A)Clone();
  32.         }
  33.         public object Clone()
  34.         {
  35.             A newA = new A();
  36.             newA.S = this.S;
  37.             newA.P =(Man) this.P.Clone();
  38.             return newA;
  39.         }
  40.     }
  41.     class Program
  42.     {
  43.         static void Main(string[] args)
  44.         {

  45.             A a1 = new A();
  46.             A a2 =(A) a1.MakeCopy();
  47.             a1.S = "a1";
  48.             a2.S = "a2";
  49.             a1.P.W = work.dancer;
  50.             a2.P.W = work.teacher;
  51.             Console.WriteLine("a1.s:{0},a2.s:{1}",a1.S,a2.S);
  52.             Console.WriteLine("a1.work:{0},a2.work:{1}", a1.P.W, a2.P.W);
  53.             Console.ReadKey();
  54.         }
  55.     }
  56. }
复制代码


作者: 寂静岭    时间: 2014-4-3 23:02
天平哥在C#上颇有悟性
作者: tmtony    时间: 2014-4-3 23:16
天平哥,这么晚还在研究啊
作者: tianping    时间: 2014-4-5 23:42
寂静岭 发表于 2014-4-3 23:02
天平哥在C#上颇有悟性

谢谢老师点拨
作者: tianping    时间: 2014-4-5 23:43
tmtony 发表于 2014-4-3 23:16
天平哥,这么晚还在研究啊

站长辛苦!




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3