diff --git a/works/charp/lab-07/lab07/Program.cs b/works/charp/lab-07/lab07/Program.cs new file mode 100644 index 0000000..2eebb4b --- /dev/null +++ b/works/charp/lab-07/lab07/Program.cs @@ -0,0 +1,235 @@ +using System; +using System.Collections.Generic; + +class Program +{ + static void Main(string[] args) + { + Console.WriteLine("=== ЗАДАНИЕ 1: ИЕРАРХИЯ И ВИРТУАЛЬНЫЕ МЕТОДЫ ==="); + + Pokemon basePokemon = new Pokemon("Дикий покемон", 1, 10); + Console.Write("Базовый вызов: "); + basePokemon.ShowInfo(); + basePokemon.Attack(); + Console.WriteLine(new string('-', 40)); + + /* + Console.WriteLine("=== ЗАДАНИЕ 2: ТУРНИР И ПОЛИМОРФИЗМ (АБСТРАКТНЫЙ КЛАСС) ==="); + // Pokemon test = new Pokemon("Абстрактный", 1, 10); // КРИТИЧЕСКАЯ ОШИБКА КОМПИЛЯЦИИ! + */ + + List pokemons = new List() + { + new Pikachu("Пикачу", 5, 100), + new Squirtle("Сквиртл", 7, 90), + new Charmander("Чармандер", 6, 80), + new Bulbasaur("Бульбазавр", 4, 95) + }; + + Console.WriteLine($"Всего покемонов в списке: {pokemons.Count}"); + Console.WriteLine("\n--- Раунд 1 ---"); + + foreach (Pokemon pokemon in pokemons) + { + pokemon.ShowInfo(); + pokemon.Attack(); + } + + Console.WriteLine($"\nВсего атак проведено: {Pokemon.TotalAttacks}"); + + + Console.WriteLine("\n=== ЗАДАНИЕ 3: СПОСОБНОСТИ ПОКЕМОНОВ ==="); + + foreach (Pokemon pokemon in pokemons) + { + Console.Write($"\n{pokemon.Name} (Ур. {pokemon.Level}) умеет: "); + + if (pokemon is IEvolvable evolvable) + { + CapabilityCounter.EvolvableCount++; + evolvable.Evolve(); + } + + if (pokemon is IFlyable flyable) + { + CapabilityCounter.FlyableCount++; + flyable.Fly(); + } + + if (pokemon is not IEvolvable && pokemon is not IFlyable) + { + Console.WriteLine(" -> Особых способностей пока нет (только атака)."); + } + } + + Console.WriteLine("\nИтого:"); + Console.WriteLine($"Эволюционируют: {CapabilityCounter.EvolvableCount}"); + Console.WriteLine($"Летают: {CapabilityCounter.FlyableCount}"); + + Console.ReadLine(); + } + + // ИНТЕРФЕЙСЫ (ЗАДАНИЕ 3) + interface IEvolvable + { + void Evolve(); + } + + interface IFlyable + { + void Fly(); + } + + static class CapabilityCounter + { + public static int EvolvableCount { get; set; } + public static int FlyableCount { get; set; } + } + + class Pokemon + { + private static int totalAttacks = 0; + + public static int TotalAttacks + { + get { return totalAttacks; } + set { totalAttacks = value; } + } + + protected int hp; + public int HP + { + get { return hp; } + set + { + if (value < 0) hp = 0; + else if (value > 100) hp = 100; + else hp = value; + } + } + + public int Level { get; set; } + public string Name; + + public Pokemon(string name, int level, int hp) + { + this.Name = name == "" ? "Безымянный" : name; + this.Level = level; + this.HP = hp; + } + + public virtual void Attack() + { + Console.WriteLine("Покемон атакует!"); + } + + public void ShowInfo() + { + Console.Write($"{Name} (Ур. {Level}): "); + } + } + + + /* + // ВАРИАНТ ДЛЯ ЗАДАНИЯ 2: АБСТРАКТНЫЙ БАЗОВЫЙ КЛАСС С АБСТРАКТНЫМ МЕТОДОМ + abstract class Pokemon + { + private static int totalAttacks = 0; + + public static int TotalAttacks + { + get { return totalAttacks; } + set { totalAttacks = value; } + } + + protected int hp; + public int HP + { + get { return hp; } + set + { + if (value < 0) hp = 0; + else if (value > 100) hp = 100; + else hp = value; + } + } + + public int Level { get; set; } + public string Name; + + public Pokemon(string name, int level, int hp) + { + this.Name = name == "" ? "Безымянный" : name; + this.Level = level; + this.HP = hp; + } + + // МЕТОД АБСТРАКТНЫЙ: У него НЕТ ТЕЛА. Только точка с запятой! + public abstract void Attack(); + + public void ShowInfo() + { + Console.Write($"{Name} (Ур. {Level}): "); + } + } + */ + + class Pikachu : Pokemon + { + public Pikachu(string name, int level, int hp) : base(name, level, hp) { } + + public override void Attack() + { + Pokemon.TotalAttacks++; + Console.WriteLine("Пика-пика! Удар током!"); + } + } + + class Charmander : Pokemon, IEvolvable + { + public Charmander(string name, int level, int hp) : base(name, level, hp) { } + + public override void Attack() + { + Pokemon.TotalAttacks++; + Console.WriteLine("Чар-чар! Огненный хвост!"); + } + + public void Evolve() + { + Console.WriteLine(" -> Чармандер готовится к эволюции!"); + } + } + + class Squirtle : Pokemon + { + public Squirtle(string name, int level, int hp) : base(name, level, hp) { } + + public override void Attack() + { + Pokemon.TotalAttacks++; + Console.WriteLine("Сквирт-сквирт! Водяной пистолет!"); + } + } + + class Bulbasaur : Pokemon, IFlyable, IEvolvable + { + public Bulbasaur(string name, int level, int hp) : base(name, level, hp) { } + + public override void Attack() + { + Pokemon.TotalAttacks++; + Console.WriteLine("Бульба! Листовой удар!"); + } + + public void Fly() + { + Console.WriteLine(" -> Бульбазавр взлетает на лозах!"); + } + + public void Evolve() + { + Console.WriteLine("\n -> Бульбазавр готовится к эволюции!"); + } + } +} \ No newline at end of file