如何在 .net 中使用带有接口的静态列表?

huangapple go评论132阅读模式
英文:

How to use static List with interface in .net?

问题

我想在.NET中使用一个带有接口的静态列表

我想知道在代码中应该在哪里声明我的列表:在接口类中,在派生类中还是其他地方?

目前,我将它声明在派生类(BacFlo)中。

我的问题是:我无法正确地填充列表,它似乎是空的,而不应该为空。

以下是我的代码摘录:

接口:

public interface Serre
{
    void Plant(Plante p);
    void Remove(string s);
    void Water(string s);
    void Clone(string s);
    void Inventory();
}

派生类#1:

public class BacFlo : Serre
{
    static List<Plante> Garden = new List<Plante>();
}

我的主类:

Serre bc = new BacCro();
Serre bf = new BacFlo();

for (int i = 0; i < 10; i++)
{
    Plante p = new Plante("Plante commune", "Vivace", "Une plante", 120);
    bf.Plant(p);
    bc.Plant(p);
}

for (int i = 0; i < 10; i++)
{
    Plante p = new Plante("Plante rare", "à bulbe", "Une plante", 10);
    bf.Planter(p);
    bc.Planter(p);
}

bf.Inventory();
bc.Inventory();
}

void Serre.Inventory()
{
    Console.WriteLine("");
    Console.WriteLine("Inventaire du bac de floraison :");
    foreach (Plante p in Jardin)
    {
        Console.WriteLine("Plante de nom " + p.Name + ", de type " + p.Type + ", de description " + p.Description + ", de taille " + p.Taille + ", Arrosée : " + (p.IsWet == true ? "Oui" : "Non"));
    }
}

我想要实现的是使用for循环正确填充列表,并使用适当的方法显示库存。

我不确定静态列表的实例化方式...

英文:

I want to use a static List with an interface in .net

I wonder where in the code I should declare my List : in the interface class, in the derived classes, or somewhere else ?

For the moment I have it declared in the derived class (BacFlo).

My problem is : I can't populate the List correctly, it seems like the List is empty when it shouldn't.

Here is an extract of my code :

Interface :

 public interface Serre
    {
        void Plant(Plante p);
        void Remove(string s);
        void Water(string s);
        void Clone(string s);
        void Inventory();
    }

Derived Class #1 :

public class BacFlo : Serre
    {
        static List&lt;Plante&gt; Garden = new List&lt;Plante&gt;();

        }

My main class :

    Serre bc = new BacCro();
    Serre bf = new BacFlo();

for (int i = 0; i &lt; 10; i++)
        {
            Plante p = new Plante(&quot;Plante commune&quot;, &quot;Vivace&quot;, &quot;Une plante&quot;, 120);
            bf.Plant(p);
            bc.Plant(p);
        }

        for (int i = 0; i &lt; 10; i++)
        {
            Plante p = new Plante(&quot;Plante rare&quot;, &quot;&#224; bulbe&quot;, &quot;Une plante&quot;, 10);
            bf.Planter(p);
            bc.Planter(p);
        }

        bf.Inventory();
        bc.Inventory();

}

   void Serre.Inventory()
        {
            Console.WriteLine(&quot;&quot;);
            Console.WriteLine(&quot;Inventaire du bac de floraison :&quot;);
            foreach (Plante p in Jardin)
            {
                Console.WriteLine(&quot;Plante de nom &quot; + p.Name + &quot;, de type &quot; + p.Type + &quot;, de description &quot; + p.Description + &quot;, de taille &quot; + p.Taille + &quot;, Arros&#233;e : &quot; + (p.IsWet == true ? &quot;Oui&quot; : &quot;Non&quot;));
        }

What I want to achieve is correctly populate the List with my for loop and have displayed the inventory with the appropriate method.

I'm not sure about the instanciation of a static List...

答案1

得分: 1

你似乎有一种“花园”概念,基本上是一组“植物”的集合。而“Serre”似乎是如何与“花园”进行交互的接口。

我不确定“BacXYZ”类族与此有何关联,但你似乎想要让“BacXYZ”类的所有(或部分)实例与同一个“花园”进行交互。(也许考虑一下以后可能想让不同组的BacXYZ对不同的花园进行操作?)

所以,对我来说自然的做法要么是将花园作为Serre方法的参数,要么是在创建时将花园注入到每个BacXYZ实例中。

由于我怀疑你可能倾向于后者:

你可以这样做:

public class BacFlo : Serre
{
    private readonly ICollection&lt;Plant&gt; _garden;
    
    public BacFlo(ICollection&lt;Plant&gt; garden)
    {
        ArgumentNullException.ThrowIfNull(garden);
        _garden = garden;
    }

    // Serre-Implementations here
}

现在,在调用者中:

public void Main()
{
    var garden = new List&lt;Plant&gt;(); // 也可以是类字段或其他
    
    var bac1 = new BacFlo(garden);
    var bac2 = new BacFlo(garden);
    var bac3 = new BacFlo(garden);
}

^^ 所有这些都将在同一个“garden”列表上操作。不需要static

英文:

What it seems you have is some sort of Garden concept, which is basically a collection of Plants. And Serre seems to interface how to interact with Gardens.

I am not sure about how the BacXYZ family of classes is related to this, but you seem to want a group (if not all) instances of BacXYZ classes to interact with the same Garden. (Maybe think about that you later on may want different groups of BacXYZ to act upon different Gardens?)

So, the natural thing for me would be to either

  • include the Garden as an argument to Serre's methods or
  • inject the Garden to each BacXYZ instance upon creation.

As I suspect you will be leaning to the latter one:

You could do this:

public class BacFlo : Serre
{
    private readonly ICollection&lt;Plant&gt; _garden;
    
    public BacFlo( ICollection&lt;Plant&gt; garden )
    {
        ArgumentNullException.ThrowIfNull(garden);
        _garden = garden;
    }

    // Serre-Implementations here
}

Now, in the caller:

public void Main()
{
    var garden = new List&lt;Plant&gt;(); // Could also be a class field or whatever

    var bac1 = new BacFlo(garden);
    var bac2 = new BacFlo(garden);
    var bac3 = new BacFlo(garden);
}

^^ All of these would operate on the same garden list. No static needed.

答案2

得分: 0

这里是您要翻译的内容:

这个关键字“static”的使用对我来说似乎是不必要的。在大多数情况下,类中的根变量成员被声明为Public、Private或Protected。

这里 是与此相关的Stack Overflow问题。

如果是您,我会使用Private而不是Static。静态变量的行为可能对编程逻辑产生奇怪的影响。关键字Static的使用在函数/过程范围内时通常有一些意义,但它的使用是边缘的。我无法想象在类“Static”中声明List的实例的原因,您可能需要在决定是否使用“Static”之前进行一些关于Static、Private、Public和Protected的研究。

英文:

This usage of keyword "static" seems unnecessary to me. In most cases root variable members in classes are declared Public, Private or Protected.

Here is related question on Stack Overflow

Being you I would used Private instead of Static. Behaviour of Static variables may have strange effects on programming logic. Using keyword Static mostly have some sense when in the function / procedure scope, but its usage is marginal. I can't imagine reason for declaring instance of List in class Static, you may need to do some research on Static, Private, Public and Protected, before you decide e.g. for using Static

huangapple
  • 本文由 发表于 2023年6月29日 15:15:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定