Организация иерархии классов с возможностью последующего отображения - C#
Формулировка задачи:
Вот такой вопрос,как лучше создать.
К примеру у меня есть четыре поля(string,int,int,int.).Имена значений полей имя,сила,ловкость,интеллект.
У меня есть к примеру 2 и больше видов животных(пес,кот,енот и суслик).У каждого свое имя и характеристики. Дальше эти характеристики влияют на действия или еще на что либо.Как мне прописать так, чтобы не писать к каждому зверьку свою переменную силы или ловкости?Что тут и как лучше использовать?
И вот к примеру отображение в виндовс форм. У меня есть четыре label и button. При нажатие на кнопку отображается случайное животное,его имя и характеристики данного зверька.
Вот так примерно выглядит начало.Есть ли рекомендации или предложение по исправлению,оптимизации данного индусского коду?
Листинг программы
- class MyClass
- {
- private string name;
- private int stranght;
- private int intellect;
- private int agility;
- public void MyClass(string inName, int inStranght, int inIntellect, int inAgility)
- {
- name = inName;
- stranght = inStranght;
- intellect = inIntellect;
- agility = inAgility;
- }
- public string Name()
- { return name; }
- public int Stranght()
- { return stranght; }
- public int Intellect()
- { return intellect; }
- public int Agility()
- { return agility; }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Random MyRand = new Random();
- MyClass My = new MyClass();
- int MyAnimal = MyRand.Next(0, 4);
- switch (MyAnimal)
- {
- case 0:
- My.MyClass("Dog", 15, 14, 10);
- break;
- case 1 :
- My.MyClass("Cat", 5, 12, 10);
- break;
- case 2:
- My.MyClass("Raccoon", 9, 13, 14);
- break;
- case 3:
- My.MyClass("gopher", 10, 11, 11);
- break;
- }
- Console.WriteLine("Имя {0}/nсила{1}/nловкость{2}/nинтиллект{3}"My.Name,My.Stranght,My.Agility,My.Intellect);
- }
Решение задачи: «Организация иерархии классов с возможностью последующего отображения»
textual
Листинг программы
- class Animal
- {
- protected string name;
- protected int strength;
- protected int intellect;
- protected int agility;
- public Animal(string inName, int inStrength, int inIntellect, int inAgility)
- {
- name = inName;
- strength = inStrength;
- intellect = inIntellect;
- agility = inAgility;
- }
- public override string ToString()
- {
- get { return String.Format("Имя {0}/nсила{1}/nловкость{2}/nинтеллект{3}", name, strength, intellect, agility); }
- }
- public string Name { get { return name; } }
- public int Strength { get { return strength; } }
- public int Intellect { get { return intellect; } }
- public int Agility { get { return agility; } }
- }
- class Dog : Animal
- {
- public Dog(string inName, int inStrength, int inIntellect, int inAgility) : base(inName, inStrength, inIntellect, inAgility)
- {
- }
- }
- class Cat : Animal
- {
- public Cat(string inName, int inStrength, int inIntellect, int inAgility) : base(inName, inStrength, inIntellect, inAgility)
- {
- }
- }
- class Raccoon : Animal
- {
- public Raccoon(string inName, int inStrength, int inIntellect, int inAgility) : base(inName, inStrength, inIntellect, inAgility)
- {
- }
- }
- class Gopher : Animal
- {
- public Gopher(string inName, int inStrength, int inIntellect, int inAgility) : base(inName, inStrength, inIntellect, inAgility)
- {
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Random MyRand = new Random();
- Animal My = null;
- int MyAnimal = MyRand.Next(0, 4);
- switch (MyAnimal)
- {
- case 0:
- My = new Dog("Dog", 15, 14, 10);
- break;
- case 1 :
- My = new Cat("Cat", 5, 12, 10);
- break;
- case 2:
- My = new Raccoon("Raccoon", 9, 13, 14);
- break;
- case 3:
- My = new Gopher("gopher", 10, 11, 11);
- break;
- }
- Console.WriteLine(My);
- //label1.Text = My.ToString(); // WinForms
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д