使用所有存储的值克隆实例 [英] Make a clone of an instanced with all the stored values

查看:91
本文介绍了使用所有存储的值克隆实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两类,一类称为Warehouse,一类称为Warehouselocations。
WareHouse目前可以在仓库中创建,存储和查找盒子。

I have two classes one called warehouse and one called Warehouselocations. The wareHouse is currently able to create,store and find boxes in warehouselocation.

但是现在我还需要仓库才能创建的克隆版本。 WareHouseLocation以及所有存储的信息。

But now i also need the warehouse to be able to create a cloned version of wareHouseLocation with all the stored information.

   locations = new List<WareHouseLocation>();

这是我存储所有信息的列表。我希望能够复制它。

This is the list where i store all the information. I want to be able to copy it.

我试图找到自己的答案,甚至尝试了一些代码,但到目前为止,我什么都没有正常工作。

I tried to find the answer my self and even tried some code but so far i had got nothing that works properly.

    public WareHouseLocation DeepCopy() 
    {
        foreach (WareHouseLocation  wareHouseLocation in locations)
            {
                if(wareHouseLocation == null) 
                {
                    return null;
                }
                else 
                {
                   //Need code here
                }
              }
           return null;
        }

该代码当前在WareHouse类中。
我很高兴能为我提供帮助。

The code is currently in the wareHouse class. I be happy for anything that could help me.

    public class WareHouseLocation
    {
        public int FloorID { get; set; }
        public List<I3DStorageObject> storage = new List<I3DStorageObject>();


        public double MaxVolume;
        public double MaxWeight;

        public WareHouseLocation(double height, double width, double depth)
        {
            MaxVolume = height * width * depth;
            MaxWeight = 1000;
        }


        public bool hasAvailableVolumeForObject(I3DStorageObject s)
        {
            double currentVolume = 0;


            foreach (I3DStorageObject obj in storage)
            {
                currentVolume += obj.Volume;
            }

            double available = MaxVolume - currentVolume;


            if (s.Volume <= available)
            {
                return true;
            }
            else
            {
                return false;
            }

        }



    }

这是WareHouseLocation的代码。

Here is the code for the WareHouseLocation.

推荐答案

您可以通过实现< a href = https://docs.microsoft.com/zh-cn/dotnet/api/system.icloneable?view=netframework-4.8 rel = nofollow noreferrer> ICloneable 接口:

You can achieve it by implementing ICloneable interface:

public class WareHouseLocation : ICloneable
{
    public int FloorID { get; set; }
    public List<I3DStorageObject> storage = new List<I3DStorageObject>();

    public double MaxVolume;
    public double MaxWeight;

    //rest of code

    public object Clone()
    {
        var copy = (WareHouseLocation)MemberwiseClone();
        copy.storage = storage.Select(item => (I3DStorageObject)item.Clone()).ToList();

        return copy;
    }
}

因为您有列表 WareHouseLocation 中的$ c>引用类型中,您还需要通过为<$实施 ICloneable 来正确克隆此类型。 c $ c> I3DStorageObject 也是如此,因为 MemberwiseClone 仅复制引用,而不复制引用的对象本身

Since you have a List reference type inside WareHouseLocation, you'll need to properly clone this as well by implementing ICloneable for I3DStorageObject as well, because MemberwiseClone copy the reference only, not the referred object itself

public class I3DStorageObject : ICloneable
{
    public double Volume { get; set; }
    public object Clone()
    {
        return MemberwiseClone();
    }
}

您也可以看看 MemberwiseClone 有关对象的深/浅复制的详细信息和示例

You can also have a look at MemberwiseClone for details and examples of deep/shallow copy of objects

这篇关于使用所有存储的值克隆实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆