列表类的类结构?

发布于 2024-10-12 23:33:27 字数 866 浏览 3 评论 0原文

我正在寻找一些类结构帮助。假设我有一个名为 Dog 的类,保存有关狗的信息(名称、体重、类型),但因为可能有多只狗,所以我还希望有一个类来保存所有这些狗,以便在我的整个项目中使用。执行此操作的最佳方法是什么?

只是为了有一个 DogList 类,将 Dog 类信息存储到公共列表中?允许我随意取回吗?

或者该列表应该是原始狗类中的静态列表?也许在构造函数中,每当有人创建一条新狗时,该狗就会被放入静态列表中?

编辑:抱歉问题有点错过引导 这是我到目前为止的结构,想知道是否有更好的方法来实现。

public class Dog
{
    public string name{get;set;}
    public int weight { get; set; }
}

public class DogFactory //not sure if thats the correct wording
{
    public List<dog> lstDogs = new List<dog>();
    public void setDogs()
    {
    Animal.Retrieve("Dog"); 
    //will retrieve a list of all dogs, with details, though this is costly to use
        foreach(Animal.Dog pet in Animal._Dogs)
        {
            Dog doggie = new doggie();
            doggie.Name = pet.Name;
            ...etc
            lstDog.add(doggie);
        }
    }
}

I am looking for some class structure help. Lets say I have a class called Dog holds information about the dog (name, weight, type) but because there could be multiple dogs, I would also like a class to hold all of these dogs, for use throughout my entire project. Whats the best way to go about doing this?

Just to have a DogList class, that stores the Dog class information into a public list? Allowing me to retrieve it at will?

Or should the list be a static within the original dog class? maybe in the constructor, any time someone creates a new dog, the dog gets put into the static list?

Edit: Sorry question was a bit miss leading
Here is the structure I have so far, wondering if there is a better way to implement.

public class Dog
{
    public string name{get;set;}
    public int weight { get; set; }
}

public class DogFactory //not sure if thats the correct wording
{
    public List<dog> lstDogs = new List<dog>();
    public void setDogs()
    {
    Animal.Retrieve("Dog"); 
    //will retrieve a list of all dogs, with details, though this is costly to use
        foreach(Animal.Dog pet in Animal._Dogs)
        {
            Dog doggie = new doggie();
            doggie.Name = pet.Name;
            ...etc
            lstDog.add(doggie);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫﹏色ふ单纯 2024-10-19 23:33:27

创建狗列表的最简单方法是:

List

这是使用 System.Collections 中的 List 类的强类型狗列表。通用的。您可以创建一个这样的类:

public class DogList : List

虽然这通常是不必要的,除非您想专门向 Dogs 列表添加属性而不是狗本身。

更新:

使用 List 时通常不需要创建专用列表类。在大多数情况下,您可以这样做:

public class DogService
{
    public List<Dog> GetAllDogs()
    {
        var r = new Repository(); // Your data repository
        return r.Dogs.ToList();   // assuming your repository exposes a 'Dogs query'
    }
}

如果您使用 LINQ 获取数据,则可以使用 ToList() 扩展返回 List,因此无需创建类派生自 List

The easiest way to make a list of dogs is:

List<Dog>

This is a strongly-typed list of Dogs using the List<T> class from System.Collections.Generic. You can make a class of this:

public class DogList : List<Dog>

Though this is usually not necessary unless you want to add properties specifically to a list of Dogs and not the dogs themselves.

UPDATE:

You usually do not need to create a dedicated list class when using List<T>. In most cases, you can do this:

public class DogService
{
    public List<Dog> GetAllDogs()
    {
        var r = new Repository(); // Your data repository
        return r.Dogs.ToList();   // assuming your repository exposes a 'Dogs query'
    }
}

If you're using LINQ to get your data, you can use the ToList() extension to return a List<T>, so no need to create a class that derives from List<T>.

<逆流佳人身旁 2024-10-19 23:33:27

嗯,C# 已经有一个非常好的 List<> 类,所以除非您有非常具体的需求,否则我不会使用新的 DogList 类。如果您需要跟踪所有创建的狗,那么狗类中的静态列表就可以了。只需确保它保持私有并且您有添加或检索狗的方法即可。更好的是,您的 Dog 工厂可以将它们添加到您的列表中(我现在看到您自己提到过)。

Well, C# has a perfectly good List<> class already, so unless you have very specific needs I wouldn't go with a new DogList class. If you need to keep track of all created Dogs, then a static list inside the dog class is fine. Just make sure it's kept private and you have methods for adding or retrieving dogs. Better yet, your Dog factory can add them to the list for you (which I now see you mentioned yourself).

甜`诱少女 2024-10-19 23:33:27

或者列表应该是静态的
原来的狗类?也许在
构造函数,任何时候有人创建
一只新狗,狗被放入
静态列表?

该解决方案适用于单线程应用程序,但它会让您面临全局变量的所有危险。

这也是一个糟糕的抽象*:什么时候 Dog 类包含任何调用者在运行时构造的所有狗的列表才有意义?如果调用者取消分配 Dog 实例,会发生什么?

相反,将使用狗列表的呼叫者应明确承担创建和管理该列表的责任。这些调用者将知道列表的正确生命周期,他们将能够控制列表中的内容,而且对于任何阅读代码的人来说,发生了什么以及如何发生都是显而易见的。

* 除非列表应该是创建的狗的日志。

Or should the list be a static within
the original dog class? maybe in the
constructor, any time someone creates
a new dog, the dog gets put into the
static list?

This solution would work in a single-threaded application, but it would expose you to all the dangers of global variables.

It would also be a poor abstraction*: when would it make sense for a Dog class contain a list of all the dogs any caller has constructed at runtime? What's supposed to happen if the caller de-allocates a Dog instance?

Instead, callers that will use a list of dogs should explicitly take responsibility for creating and managing it. Those callers will know the proper lifetime for the list, they'll be able to control what goes in the list, plus it will be obvious to anyone reading the code what's happening and how.

* Unless the list is supposed to be a log of dogs created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文