设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 4624|回复: 4
打印 上一主题 下一主题

C#深复制的测试

[复制链接]
跳转到指定楼层
1#
发表于 2014-4-3 22:51:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 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. }
复制代码

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2014-4-3 23:02:24 | 只看该作者
天平哥在C#上颇有悟性

点击这里给我发消息

3#
发表于 2014-4-3 23:16:10 | 只看该作者
天平哥,这么晚还在研究啊
4#
 楼主| 发表于 2014-4-5 23:42:36 | 只看该作者
寂静岭 发表于 2014-4-3 23:02
天平哥在C#上颇有悟性

谢谢老师点拨
5#
 楼主| 发表于 2014-4-5 23:43:00 | 只看该作者
tmtony 发表于 2014-4-3 23:16
天平哥,这么晚还在研究啊

站长辛苦!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-4-29 23:31 , Processed in 0.091313 second(s), 28 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表