首页 > 其他分享 >深拷贝与浅拷贝

深拷贝与浅拷贝

时间:2022-12-14 20:56:45浏览次数:44  
标签:cabinetNum Console cabinetNumClone WriteLine GetHashCode 拷贝 CabinetNumber

 

在C#中的ICloneable中给出的Clone方法只是一个模棱两可的方法,并不足以实现深拷贝和浅拷贝。

 

深拷贝:

 

using(Stream objectStream = new MemoryStream())
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(objectStream, this);
objectStream.Seek(0, SeekOrigin.Begin);
return formatter.Deserialize(objectStream) as Test;
}

 

使用using开出一段流,将对象序列化。这里没有成功,有兴趣的可以试下。

 

 

深拷贝与浅拷贝的根本区别就在于:是否拷贝的是值还是地址。

浅拷贝是两个对象指向同一片地址。

深拷贝是两个对象两个完全不同的地址,但是里面的内容一样。

我的思路是:A类中的字段是B类的对象。直接拷贝A类中的属性,属性是字段的话,重新申明一个B类对象,将A类中B的对象里的拷贝进去。

 

class Number
    {
        public string number { get; set; }

        public override string ToString()
        {
            return this.number;
        }
        public Number()
        {
            this.number = string.Empty;
        }

        public void NumberClone(Number num)
        {
            this.number = num.number;
        }
    }

 在这里,属性不能自己调用自己。如果调用则会陷入死循环。

 public Number CabinetNumber
{
  get{ return CabinetNumber;}
  set{ }
}
class Cabinet
    {
        public int No { get; set; }
        public int Side { get; set; }
        public string Name { get; set; }

        private Number _number;
        public Number CabinetNumber
        {
            //get; set;
            get
            {
                if (_number == null)
                {
                    _number = new Number();
                }
                return _number;
            }
            set { }
        }

        public void CreateCabinet(Cabinet cabinet)
        {
            this.No = cabinet.No;
            this.Side = cabinet.Side;
            this.Name = cabinet.Name;
            Console.WriteLine("this.GetHashCode: " + this.GetHashCode());
            Console.WriteLine();
        }
        public Cabinet CreateCabinetNumber(Cabinet cabinet)
        {
            //this.No = cabinet.No;
            //this.Side = cabinet.Side;
            //this.Name = cabinet.Name;
            //this.CabinetNumber.NumberClone(cabinet.CabinetNumber);
            Cabinet cabinetNum = new Cabinet() { CabinetNumber = new Number { } };
            cabinetNum.No = cabinet.No;
            cabinetNum.Side = cabinet.Side;
            cabinetNum.Name = cabinet.Name;
            cabinetNum.CabinetNumber.NumberClone(cabinet.CabinetNumber);

            Console.WriteLine("CreateCabinetNumber里clone: ");
            Console.WriteLine("this.GetHashCode: " + cabinetNum.GetHashCode());
            Console.WriteLine("this.CabinetNumber.GetHashCode: " + cabinetNum.CabinetNumber.GetHashCode());
            Console.WriteLine();
            return cabinetNum;
        }
        public override string ToString()
        {
            if (this.CabinetNumber == null)
            {
                return string.Format("{0},{1},{2}", this.No, this.Name, this.Side);
            }
            return string.Format("{0},{1},{2},{3}", this.No, this.Name, this.Side, this.CabinetNumber.ToString());
        }
    }

主程序代码:

            Console.WriteLine("-----CabinetNumber----");
            Cabinet cabinetNum = new Cabinet() { No = 12, Name = "cabinet01", Side = 12, CabinetNumber = new Number() { number = "num01" } };
            Cabinet cabinetNumClone = new Cabinet() { CabinetNumber = new Number() };
            Console.WriteLine("cabinetNum.GetHashCode: " + cabinetNum.GetHashCode());
            Console.WriteLine("cabinetNumClone.GetHashCode: " + cabinetNumClone.GetHashCode());
            Console.WriteLine();

            Console.WriteLine("cabinetNum.CabinetNumber.GetHashCode: " + cabinetNum.CabinetNumber.GetHashCode());
            //Console.WriteLine("cabinetNumClone.CabinetNumber.GetHashCode: " + cabinetNumClone.CabinetNumber.GetHashCode());
            Console.WriteLine();
            Console.WriteLine("--测试---");
            //cabinetNumClone = Cabinet.CreateCabinetNumber(cabinetNum);
            cabinetNumClone = cabinetNumClone.CreateCabinetNumber(cabinetNum);


            Console.WriteLine("cabinetNum.CabinetNumber.GetHashCode: " + cabinetNum.CabinetNumber.GetHashCode());
            Console.WriteLine("cabinetNumClone.CabinetNumber.GetHashCode: " + cabinetNumClone.CabinetNumber.GetHashCode());
            Console.WriteLine();

            Console.WriteLine("cabinetNum: " + cabinetNum.ToString());
            Console.WriteLine("cabinetNumClone: " + cabinetNumClone.ToString());
            Console.WriteLine();

            Console.WriteLine("-----改变cabinetNumClone------");
            cabinetNumClone.CabinetNumber.number = "num01Clone";
            cabinetNumClone.Name = "cabinetClone";
            Console.WriteLine("cabinetNum.GetHashCode: " + cabinetNum.GetHashCode());
            Console.WriteLine("cabinetNumClone.GetHashCode: " + cabinetNumClone.GetHashCode());
            Console.WriteLine();

            Console.WriteLine("cabinetNum.CabinetNumber.GetHashCode: " + cabinetNum.CabinetNumber.GetHashCode());
            Console.WriteLine("cabinetNumClone.CabinetNumber.GetHashCode: " + cabinetNumClone.CabinetNumber.GetHashCode());
            Console.WriteLine();
            Console.WriteLine("cabinetNum: " + cabinetNum.ToString());
            Console.WriteLine("cabinetNumClone: " + cabinetNumClone.ToString());

 

标签:cabinetNum,Console,cabinetNumClone,WriteLine,GetHashCode,拷贝,CabinetNumber
From: https://www.cnblogs.com/wen-chen/p/16983444.html

相关文章

  • 【Java】Java 中实现深拷贝
    浅拷贝和深拷贝浅拷贝:只复制一层,如果对象的属性是引用数据类型,只会复制属性内存地址。深拷贝:不只复制一层,如果对象属性是引用数据类型,会继续向下进行复制。深拷贝的实......
  • 深拷贝和浅拷贝
    在C#中的ICloneable中给出的Clone方法只是一个模棱两可的方法,并不足以实现深拷贝和浅拷贝。深拷贝:using(StreamobjectStream=newMemoryStream()){IFormatterform......
  • C#深拷贝有哪些方法实现
    概述为什么要用到深拷贝呢?比如我们建了某个类Person,并且实例化出一个对象,然后,突然需要把这个对象复制一遍,并且复制出来的对象要跟之前的一模一样,来看下我们一般......
  • Eigen::Map 拷贝
    Map是Eigen中一个非常有用却又很容易被忽略的类,可以用它避免很多不必要的内存拷贝,举个例子,你手头有一个std::vector<Eigen::Vector3d>pts的数据,你需要将这个数据转化成一......
  • c++中的构造函数,拷贝构造函数和赋值函数
    1、拷贝构造和赋值函数的区别:1)拷贝构造函数是一个对象初始化一块内存区域,这块内存就是新对象的内存区,而赋值函数是对于一个已经被初始化的对象来进行赋值操作。2)一般来说......
  • 拷贝函数strncpy/snprintf/memcpy性能分析
    目录​​strncpy​​​​snprintf实现拷贝​​​​memcpy​​​​dpdk的rte_memcpy​​​​参考​​strncpysnprintf实现拷贝memcpydpdk的rte_memcpy参考strncpy和snp......
  • 第五章 构造、析构、拷贝语意学
    第五章构造、析构、拷贝语意学纯虚函数的存在在虚基类的时候一定要将析构函数声明为虚函数。编译器在调用派生类析构函数会采用静态调用的方式一层一层的调用每一个虚基......
  • C#深拷贝方法
    概述 为什么要用到深拷贝呢?比如我们建了某个类Person,并且实例化出一个对象,然后,突然需要把这个对象复制一遍,并且复制出来的对象要跟之前的一模一样,来看下我们一般......
  • SCP后台远程拷贝
    SCP后台远程拷贝需求如果直接使用scp拷贝文件,终端断开以后,scp命令就停止了,容易拷到一半就失败了,需要重新拷贝,因此直接后台运行scp命令。步骤正常scp命令运行拷贝scp......
  • cpp浅拷贝和深拷贝
    一、浅拷贝Shallowcopy:ifthefieldisapointertosomeobject,theaddressofthepointeriscopiedratherthanitscontents.(浅拷贝:数据域是一个指针,只拷指......