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

问题描述

好的,我正在做一项涉及动物汽车旅馆的大学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

推荐答案

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?



propertGrid1.SelectedObject = animalObj;





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



您还可以通过添加 []。


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

08-30 22:45