分层类设计

发布于 2024-10-11 04:19:54 字数 357 浏览 2 评论 0原文

让我们考虑一下我想创建以下层次结构:

动物 ->类(哺乳动物、两栖类……)->科(猫科、犬科……)->物种(猫、虎……)。

每个子类都依赖于前一个子类。

关于创建与此类似的树状类的最佳方法有什么建议吗?

这只是我手头上的问题的一个例子,但解决方案会对我有很大帮助...

谢谢

真正的问题是这样的:

我必须解析以下类型的消息 0102060800FF。

首先,01 告诉我我有特定类型的消息。 给定该类型,我必须在特定表中查找值 02,依此类推...

我希望子层次结构的可能值由其父级过滤。我不确定我说得是否足够清楚。

谢谢

Let's consider I want to create the following hierarchy:

Animal -> Class (mammal, amphibian, ...) -> Family (Felidae, Canidae, ...) -> Species (cat, tiger, ...).

each sub class dependes on the previous one.

Any suggestion on the best methodology for creating a tree-like class similar to this one?

This is just an example of the problem I have at hands but a solution would help me a lot...

thanks

The real problem is this:

I have to parse a message of the following type 0102060800FF.

Firstly, 01 tells me I have a specific type of message.
Given that type, i must look for the value 02 in a specific table, and so on...

I want a subhierarchy's possible values filtered by its parent. I'm not sure if i'm being clear enough.

thanks

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

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

发布评论

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

评论(2

当爱已成负担 2024-10-18 04:19:54

如今,人们更喜欢组合而不是继承。这是一个示例 - 需要更多的逻辑检查以确保您添加到正确的层次结构中,等等。

public class Class
{
    private readonly IList<Animal> animals = new List<Animal>();

    public IEnumerable<Animal> Animals
    {
        get
        {
            return this.animals;
        }
    }

    public void AddAnimal(Animal animal)
    {
        if (animal == null)
        {
            throw new ArgumentNullException("animal");
        }

        this.animals.Add(animal);
    }

    //// etc.
}

public class Family
{
    private readonly IList<Animal> animals = new List<Animal>();

    public IEnumerable<Animal> Animals
    {
        get
        {
            return this.animals;
        }
    }

    public void AddAnimal(Animal animal)
    {
        if (animal == null)
        {
            throw new ArgumentNullException("animal");
        }

        this.animals.Add(animal);
    }

    //// etc.
}

public class Species
{
    private readonly IList<Animal> animals = new List<Animal>();

    public IEnumerable<Animal> Animals
    {
        get
        {
            return this.animals;
        }
    }

    public void AddAnimal(Animal animal)
    {
        if (animal == null)
        {
            throw new ArgumentNullException("animal");
        }

        this.animals.Add(animal);
    }

    //// etc.
}

public class Animal
{
    private readonly Class @class;
    private readonly Family family;
    private readonly Species species;

    public Animal(Class @class, Family family, Species species)
    {
        if (@class == null)
        {
            throw new ArgumentNullException("@class");
        }

        if (family == null)
        {
            throw new ArgumentNullException("family");
        }

        if (species == null)
        {
            throw new ArgumentNullException("species");
        }

        this.@class = @class;
        this.family = family;
        this.species = species;
        [email protected](this);
        this.family.AddAnimal(this);
        this.species.AddAnimal(this);
    }

    public Class Class
    {
        get
        {
            return this.@class;
        }
    }


    public Family Family
    {
        get
        {
            return this.family;
        }
    }


    public Species Species
    {
        get
        {
            return this.species;
        }
    }
}

These days, favor composition over inheritance. Here's an example - needs some more logic checking to make sure you're adding into the proper hierarchy, etc.

public class Class
{
    private readonly IList<Animal> animals = new List<Animal>();

    public IEnumerable<Animal> Animals
    {
        get
        {
            return this.animals;
        }
    }

    public void AddAnimal(Animal animal)
    {
        if (animal == null)
        {
            throw new ArgumentNullException("animal");
        }

        this.animals.Add(animal);
    }

    //// etc.
}

public class Family
{
    private readonly IList<Animal> animals = new List<Animal>();

    public IEnumerable<Animal> Animals
    {
        get
        {
            return this.animals;
        }
    }

    public void AddAnimal(Animal animal)
    {
        if (animal == null)
        {
            throw new ArgumentNullException("animal");
        }

        this.animals.Add(animal);
    }

    //// etc.
}

public class Species
{
    private readonly IList<Animal> animals = new List<Animal>();

    public IEnumerable<Animal> Animals
    {
        get
        {
            return this.animals;
        }
    }

    public void AddAnimal(Animal animal)
    {
        if (animal == null)
        {
            throw new ArgumentNullException("animal");
        }

        this.animals.Add(animal);
    }

    //// etc.
}

public class Animal
{
    private readonly Class @class;
    private readonly Family family;
    private readonly Species species;

    public Animal(Class @class, Family family, Species species)
    {
        if (@class == null)
        {
            throw new ArgumentNullException("@class");
        }

        if (family == null)
        {
            throw new ArgumentNullException("family");
        }

        if (species == null)
        {
            throw new ArgumentNullException("species");
        }

        this.@class = @class;
        this.family = family;
        this.species = species;
        [email protected](this);
        this.family.AddAnimal(this);
        this.species.AddAnimal(this);
    }

    public Class Class
    {
        get
        {
            return this.@class;
        }
    }


    public Family Family
    {
        get
        {
            return this.family;
        }
    }


    public Species Species
    {
        get
        {
            return this.species;
        }
    }
}
小草泠泠 2024-10-18 04:19:54

每个具体到实际动物的类都是一个抽象类。 Tiger 是一个具体类,它继承自抽象类 Felidae、抽象类 Mammalia 等。

如果你想变得更花哨,你可以使用接口,如 IPredator、IWarmBlooded;将它们定义为能力。

Every class down to the actual animal is an abstract class. Tiger is a concrete class which inherits from abstract class Felidae, from abstract class Mammalia, etc.

If you want to get really fancy, you can use interfaces, like IPredator, IWarmBlooded; define them as capabilities.

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