简体   繁体   English

如何让两个类引用相同的对象列表?

[英]How to have two classes with references to the same list of objects?

I have 3 classes (Rawmaterial, RawmaterialRepository and Storage) and I would like to know how can I do so that the Storage class has as an attribute the same list created for RawmaterialRepository?我有 3 个类(Rawmaterial、RawmaterialRepository 和 Storage),我想知道如何才能使 Storage class 具有为 RawmaterialRepository 创建的相同列表作为属性? In the class program I instantiated the wood and repository objects and added wood to the repository using the addRawmaterial method.在 class 程序中,我实例化了木材和存储库对象,并使用 addRawmaterial 方法将木材添加到存储库中。 Finally I instantiated storage1 and called the StockAlert method but it is not working for me.最后我实例化了 storage1 并调用了 StockAlert 方法,但它对我不起作用。 An error appears saying that the list in the Warehouse class is empty.出现错误提示仓库 class 中的列表为空。 This is my code.这是我的代码。

public class Rawmaterial
{
    public string name;
    public int stock;

    public Rawmaterial(string name, int stock)
    {
        this.name = name;
        this.stock = stock;
    }
}
public class RawmaterialRepository
{
    public List<Rawmaterial> listRM = new List<Rawmaterial>();
    public RawmaterialRepository()
    {
        listRM = new List<Rawmaterial>();
    }
    public void addRawmaterial(Rawmaterial rawmaterial)
    {
        listRM.Add(rawmaterial);
    }
}
public class Storage
{
    public string name;
    public RawmaterialRepository listRM;
    public Storage(string name)
    {
        this.name = name;
        listRM = ; //this is a problem
    }

    public void StockAlert()
    {
        foreach (Rawmaterial rawmaterial in listRM.listRM) //this is a problem
        {
            if (rawmaterial.stock  <= 10)
            {
                Console.WriteLine("Sotck Alert");
            } 
        }
    }
}
class Program
{
    static void Main(string[] args)
    {
        Rawmaterial wood = new Rawmaterial("wood", 300) ;
        RawmaterialRepository repository = new RawmaterialRepository();
        repository.addRawmaterial(wood);
        Storage storage1 = new Storage("storage1");
        storage1.StockAlert();
}

PS Could you help me? PS你能帮帮我吗? I'm new to this.我是新手。

You can pass your repository to storage in the storage constructor:您可以将存储库传递给存储构造函数中的存储:

public Storage(string name, RawmaterialRepository listRM)
    {
        this.name = name;
        this.listRM = listRM; //this is a problem
    }

and then in Main然后在Main

Storage storage1 = new Storage("storage1", repository );

You want to use a resource called "pass by reference"... The default behavior of C# is to pass-by-reference objects of custom classes, although you can make that explicit with the ref keyword您想使用名为“按引用传递”的资源... C# 的默认行为是按引用传递自定义类的对象,尽管您可以使用ref关键字使其明确

When you pass by reference, the object reference is passed (ie the memory location where the object is stored), such that inside the function or other class you do not create a new object, you only work with the one you had from the outside world. When you pass by reference, the object reference is passed (ie the memory location where the object is stored), such that inside the function or other class you do not create a new object, you only work with the one you had from the outside世界。

Just for reference, in python the default behavior is also pass-by-reference.仅供参考,在 python 中,默认行为也是按引用传递。

public class Storage
{
        public string name;
        public RawmaterialRepository listRM;
        public Storage(string name, ref RawmaterialRepository repository )
        {
            this.name = name;
            listRM = repository; //this will be a reference to the same memory you created outside
        }
        public void StockAlert()
        {
        foreach (Rawmaterial rawmaterial in listRM.listRM)     //this is a problem
        {
                if (rawmaterial.stock  <= 10)
                {
                    Console.WriteLine("Sotck Alert");
                } 
            }
        }
}
class Program
{
    static void Main(string[] args)
    {
        Rawmaterial wood = new Rawmaterial("wood", 300) ;
        RawmaterialRepository repository = new         RawmaterialRepository();
        repository.addRawmaterial(wood);
        Storage storage1 = new Storage("storage1", repository); // just pass the object normally
        storage1.StockAlert();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM