C#架构问题 [英] C# Architecture Question

查看:96
本文介绍了C#架构问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试构建程序时遇到问题,正在寻找建议.

我将使用示例来尝试说明我要完成的任务.

我有一个动物"基类,该基类存储每种动物都具有的属性:

I''m having an issue trying to architect a program, and am looking for suggestions.

I''ll use examples to try to illustrate what I''m trying to accomplish.

I have a base class "Animal" that stores properties that every animal has:

public class Animal: INotifyPropertyChanged
{
    #region PropertyChanged Event
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
    #endregion

    private string name;
    private string gender;

    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            OnPropertyChanged("Name");
        }
    }

    public string Gender
    {
        get { return gender; }
        set
        {
            gender = value;
            OnPropertyChanged("Gender");
        }
    }
}



然后,我还有其他实现基类动物的类:



I then have other classes that implement the base class animal:

public class Dog: Animal
{
    // Insert Dog Specific Properties and Methods Here
}





public class Skunk: Animal
{
    // Insert Skunk Specific Properties and Methods Here
}



我希望有一个对象,可以用来传递所有基类信息以及派生类信息.现在,我能想到的唯一方法是使用另一个类,该类只是一个包含所有动物的容器:



I want to have one object that I can use to pass all of the base class information, as well as derived class information. Right now the only way I can think to do this would be with another class that is just a container of all animals:

public class AnimalContainer
{
    Dog dog = new Dog();
    Skunk skunk = new Skunk();
}





Is there a better way to accomplish what I am trying to do?

推荐答案

public class AnimalCollection : List<Animal>
{
}


您可以将任何动物源对象添加到该集合中.


You can add any animal-derived object to thie collection.


尽管我不确定问题是什么,我还是会尝试一下;

如果您创建了新的Dog dog,则可以将其作为Animal传递给其他任何方法
Though i''m not sure what the question is i''ll give it a try anyway;

If you create a new Dog dog you can pass the dog to any other method as a Animal thus
 Public void DoSomething(Animal animal)
{
...
}



然后,当您实际去处理动物(听起来很脏)时,您要做的就是



Then when you actually go and do something with the animal (damn that sounds dirty) all you have to do is

Dog dog = animal as Dog;
if(null != dog)
{
   Console.WriteLine("this is a real dog")
...



如果您创建一个Animal接口并将其传递给用户方法,那就更好了.

希望这有帮助,AT



It would even be better if you''d make a Animal interface and pass that to the user method.

Hope this helps, AT


%anonymous%写道:
%anonymous% wrote:

谁建造了方舟?
Noah ,诺亚,
谁建造了方舟?
诺亚兄弟建造了方舟.

Who built the Ark?
Noah, Noah,
Who built the ark?
Brother Noah built the ark.



如果使用类AnimalContainer,则您的目的有一个问题.这很奇怪,您只需要引入每种动物物种的一个标本,而没有任何可扩展性.这适得其反.为了提高生产力,您需要每个物种都一对,并将此类重命名为Ark. :-)

这不只是一个玩笑.班级的目的和您的整个问题仍然不清楚.当然,这与您传递传递所有基类信息"的含糊愿望无关.

如果您需要一些灵活的东西,请使用约翰的建议.

—SA



You have one problem the purpose if the class AnimalContainer. This is something strange where you need only one specimen of each animal species you introduce, without any extensibility. This is counter-productive. To be productive, you need a pair of each species and rename this class to Ark. :-)

This is not just a joke. The purpose if the class and your whole question remains unclear. And it is certainly unrelated to your vague desire to "pass all of the base class information".

If you need something flexible, use the advice by John.

—SA


这篇关于C#架构问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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