处理特定的输入

本文关键字:输入 处理 | 更新日期: 2023-09-27 18:16:07

我正在做一项涉及动物旅馆的大学c#作业…

我将跳过细节,开门见山。我的输入有问题。我不确定如何处理特定的动物输入,比如鸟类的翼展,哺乳动物的牙齿数量?这是我的mainform类中的一段代码:
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;
    }
}

我在想这样的事情:

animalObj.name = txtName.Text;

但是我意识到我不能处理像这样的特定动物输入,只能处理一般的如名字年龄等。

处理特定的输入

我不是100%确定我是否理解了你的问题,但是现在开始。我猜你在设置每个动物的属性时遇到了问题,所以不会飞的动物不会有翅膀。我建议你创建一个动物超类,包含所有的通用数据/功能,然后为每个动物创建一个子类。

这是一个动物类的例子。这是所有动物的超类:

/// <summary>
/// This is an enum, that defines the different species of animals
/// </summary>
enum Species {Mammal, Fish, Insect, Bird}
/// <summary>
/// This is the base class for all animals
/// The class is abstract, so that we cant instantiated (Animal a = new Animal() is no longer possible)
/// It wouldn't make any sense to instantiate an animal (what color does and animal have, how many legs etc....) thats why its abstract
/// </summary>
abstract class Animal
{
    /// <summary>
    /// All animals has teeth, so we add this field to the animal
    /// </summary>
    private int teeth;
    /// <summary>
    /// All animals have legs, so we add this field in the animal as well
    /// </summary>
    private int legs;
    /// <summary>
    /// I'm not 100% sure about how the species should work
    /// I just implemented and enum for 3 species
    /// This can be adapted to suit your needs
    /// </summary>
    private Species species;
    /// <summary>
    /// This is the constructor of the animal. this constructor takes in 
    /// all the base values of the animal
    /// </summary>
    /// <param name="species">The current animals species</param>
    /// <param name="legs">The current animal's amount of legs</param>
    /// <param name="teeth">The current animal's amount of theeth</param>
    public Animal(Species species, int legs, int teeth)
    {
        //Sets the number of teeth on the current animal
        this.teeth = teeth;
        //Sets the number of legs on the current animal
        this.legs = legs;
        //Sets the species of the current animal
        this.species = species;
    }
}

这是一个子类的例子。在这个例子中,它是一只鸭子,你可以看到它给动物增加了翼展:

/// <summary>
/// This is a duck class, it inherits from the Animal class
/// Inheritance is done in C# by using Class A : Class B
/// The point of inheritance is to pass on members and functionality from a superclass to a sub class
/// In this case we give the duck legs, teeth and a species
/// </summary>
class Duck : Animal
{
    private int wingSpan;
    /// <summary>
    /// This is the constructor of the Duck subclass
    /// It takes in all the parameters that the super class needs +wingSpan
    /// It passes on the superclass parameters to the Animal class by calling the super class constructer with the
    /// Keyword base()
    /// </summary>
    /// <param name="wingSpan">The duck's wingspan</param>
    /// <param name="species">The species of the duck</param>
    /// <param name="legs">The amount of legs of the duck</param>
    /// <param name="teeth">The duck's amount of teeth</param>
    public Duck(int wingSpan, Species species, int legs, int teeth) : base(species,legs,teeth)
    {
        //sets the ducks number of wings
        this.wingSpan = wingSpan;
    }
}

这是如何在add animal:

中创建鸭子
 //Instantiates a duck
 Duck duck = new Duck(2, Species.Bird, 2, 0);