Организация иерархии классов с возможностью последующего отображения - C#

Узнай цену своей работы

Формулировка задачи:

Вот такой вопрос,как лучше создать. К примеру у меня есть четыре поля(string,int,int,int.).Имена значений полей имя,сила,ловкость,интеллект. У меня есть к примеру 2 и больше видов животных(пес,кот,енот и суслик).У каждого свое имя и характеристики. Дальше эти характеристики влияют на действия или еще на что либо.Как мне прописать так, чтобы не писать к каждому зверьку свою переменную силы или ловкости?Что тут и как лучше использовать?
И вот к примеру отображение в виндовс форм. У меня есть четыре label и button. При нажатие на кнопку отображается случайное животное,его имя и характеристики данного зверька.
Вот так примерно выглядит начало.Есть ли рекомендации или предложение по исправлению,оптимизации данного индусского коду?
Листинг программы
  1. class MyClass
  2. {
  3. private string name;
  4. private int stranght;
  5. private int intellect;
  6. private int agility;
  7. public void MyClass(string inName, int inStranght, int inIntellect, int inAgility)
  8. {
  9. name = inName;
  10. stranght = inStranght;
  11. intellect = inIntellect;
  12. agility = inAgility;
  13. }
  14. public string Name()
  15. { return name; }
  16. public int Stranght()
  17. { return stranght; }
  18. public int Intellect()
  19. { return intellect; }
  20. public int Agility()
  21. { return agility; }
  22. }
  23. class Program
  24. {
  25. static void Main(string[] args)
  26. {
  27. Random MyRand = new Random();
  28. MyClass My = new MyClass();
  29. int MyAnimal = MyRand.Next(0, 4);
  30. switch (MyAnimal)
  31. {
  32. case 0:
  33. My.MyClass("Dog", 15, 14, 10);
  34. break;
  35. case 1 :
  36. My.MyClass("Cat", 5, 12, 10);
  37. break;
  38. case 2:
  39. My.MyClass("Raccoon", 9, 13, 14);
  40. break;
  41. case 3:
  42. My.MyClass("gopher", 10, 11, 11);
  43. break;
  44. }
  45. Console.WriteLine("Имя {0}/nсила{1}/nловкость{2}/nинтиллект{3}"My.Name,My.Stranght,My.Agility,My.Intellect);
  46. }

Решение задачи: «Организация иерархии классов с возможностью последующего отображения»

textual
Листинг программы
  1. class Animal
  2. {
  3.     protected string name;
  4.     protected int strength;
  5.     protected int intellect;
  6.     protected int agility;
  7.  
  8.     public Animal(string inName, int inStrength, int inIntellect, int inAgility)
  9.     {
  10.         name = inName;
  11.         strength = inStrength;
  12.         intellect = inIntellect;
  13.         agility = inAgility;
  14.     }
  15.  
  16.     public override string ToString()
  17.     {
  18.         get { return String.Format("Имя {0}/nсила{1}/nловкость{2}/nинтеллект{3}", name, strength, intellect, agility); }
  19.     }
  20.  
  21.     public string Name { get { return name; } }
  22.     public int Strength { get { return strength; } }
  23.     public int Intellect { get { return intellect; } }
  24.     public int Agility { get { return agility; } }
  25. }
  26.  
  27. class Dog : Animal
  28. {
  29.     public Dog(string inName, int inStrength, int inIntellect, int inAgility) : base(inName, inStrength, inIntellect, inAgility)
  30.     {
  31.     }
  32. }
  33.  
  34. class Cat : Animal
  35. {
  36.     public Cat(string inName, int inStrength, int inIntellect, int inAgility) : base(inName, inStrength, inIntellect, inAgility)
  37.     {
  38.     }
  39. }
  40.  
  41. class Raccoon : Animal
  42. {
  43.     public Raccoon(string inName, int inStrength, int inIntellect, int inAgility) : base(inName, inStrength, inIntellect, inAgility)
  44.     {
  45.     }
  46. }
  47.  
  48. class Gopher : Animal
  49. {
  50.     public Gopher(string inName, int inStrength, int inIntellect, int inAgility) : base(inName, inStrength, inIntellect, inAgility)
  51.     {
  52.     }
  53. }
  54.  
  55. class Program
  56. {
  57.     static void Main(string[] args)
  58.     {
  59.         Random  MyRand = new Random();
  60.         Animal My = null;
  61.  
  62.         int MyAnimal = MyRand.Next(0, 4);
  63.         switch (MyAnimal)
  64.         {
  65.             case 0:
  66.                 My = new Dog("Dog", 15, 14, 10);
  67.                 break;
  68.             case 1 :
  69.                 My = new Cat("Cat", 5, 12, 10);
  70.                 break;
  71.             case 2:
  72.                 My = new Raccoon("Raccoon", 9, 13, 14);
  73.                 break;
  74.             case 3:
  75.                 My = new Gopher("gopher", 10, 11, 11);
  76.                 break;  
  77.          }
  78.          Console.WriteLine(My);
  79.          //label1.Text = My.ToString(); // WinForms
  80.     }
  81. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

9   голосов , оценка 3.889 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы