好的我正在做一项涉及动物汽车旅馆的大学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;
但意识到我无法处理像这样的特定动物输入,只有一般的名字年龄等。
答案 0 :(得分:1)
我不能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;
}
}
这就是你如何在添加动物中创建鸭子:
//Instantiates a duck
Duck duck = new Duck(2, Species.Bird, 2, 0);