处理具体的输入? [英] handle specific input?

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

问题描述

好的,我正在做一项涉及动物汽车旅馆的大学C#任务...



我会跳过细节并切入追逐......输入有问题。我不知道如何处理特定的动物输入,如鸟类的翼展,哺乳动物的牙齿数量?

这是我的主要代码的一大块代码class:



Ok I'm doing a univerity C# assignment that involves an animal motel...

I'll skip the details and cut to the chase..I'm havin problems with the input..I'm not sure how to handle the specific animal input like wingspan for birds, number of teeths for mammals?
this is a chunk of the code from my mainform class:

private void AddAnimal(){

        Animal animalObj;

            Category animalCategory = (Category)CategoryList.SelectedIndex;

            switch (animalCategory)
            {
                case Category.Mammal:
                {
                    MammalSpecies animalSpecies = (MammalSpecies) Enum.Parse(typeof (MammalSpecies), AnimalList.Text);
                    animalObj = MammalFactory.CreateMammal(animalSpecies);
                 break;
                }





我在考虑这样的事情



I was thinking of seomthing like this

animalObj.name = txtName.Text;





但是我意识到我无法处理像这样的特定动物输入,只有像名字年龄等一般..



有人可以指点我正确的方向我很困惑



but realised I cant handle the specific animal input like this only the general like name age etc..

can someone please point me in the right direction I'm confused

推荐答案

的方式我要做的就是首先分离你的顾虑。

首先从添加动物方法中删除对控件和其他UI元素的所有崇敬,然后将相应的数据作为参数传递。然后在创建它时从方法返回Animal对象。



所以你的方法将成为

The way I would do it is to start by separating your concerns.
Start by removing all reverences to controls and other UI elements from your "Add Animal" method and pass in the appropriate data as parameters. Then return the Animal object from the method when you have created it.

So your method would become
private Animal AddAnimal( string animalName, Category category)
   {
   ...

这样,你将那些变化很大的东西(UI)与变化不大的东西分开(动物)



然后,我将介绍如何创建一个Abstract Animal类,并从中派生类别,例如Mammal,Reptile等。可能,你也想制作那些抽象的,并从哺乳动物中获取猫科动物,牛等等 - 但这可能适用于以后。

那样,你可以开始对你的构造函数进行分解:

That way, you are separating stuff that changes a lot (the UI) from stuff that doesn't change much (Animals)

Then, I'd look at creating an Abstract Animal class, and deriving categories from that, such as Mammal, Reptile, and so on. Possibly, you will want to make those abstract as well and derive Feline, Bovine, etc from Mammal - but that may be for later.
That way, you can start decomplicating your constructor:

private Animal AddAnimal( string animalName, Category category)
   {
   switch (category)
      {
      case Category.Mammal: return new Mammal(animalName);
      case Category.Reptile: return new Reptile(animalName);
      ...



这是否有意义,还是比你的课程更进一步?


Does that make sense, or is it a bit further than your course has got yet?


通常多态性 [ ^ ]是开关控件的面向对象编程重新定位声明。
Usually polymorphism[^] is the object oriented programming repacement of the switch control statement.


您可以尝试 PropertyGrid [ ^ ] class。



例如,你有一个名为的变量propertyGrid1 。然后为当前对象分配属性 SelectedObject

You can try the PropertyGrid[^] class.

For example, you have a variable called propertyGrid1. Then you assign your current object the property SelectedObject.
propertGrid1.SelectedObject = animalObj;





当前班级的所有公共物业都会显示在物业网格中。



您还可以通过添加等属性来使其更漂亮。 DescriptionAttribute [ ^ ]。


这篇关于处理具体的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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