This commit is contained in:
2026-10-09 15:56:39 +03:00
parent 9f08efb21b
commit 8a7eed88c3
+106 -19
View File
@@ -2,9 +2,63 @@
namespace lab06
{
// class Pokemon
// {
//
// private string name;
// private int level;
// private int hp;
//
//
// public string Name
// {
// get { return name; }
// set { name = value; }
// }
//
// public int Level
// {
// get { return level; }
// set { level = value; }
// }
//
// public int HP
// {
// get { return hp; }
// set { hp = value; }
// }
//
//
// public Pokemon(string name, int level, int hp)
// {
// this.name = name;
// this.level = level;
// this.hp = hp;
// }
//
//
// public void ShowInfo()
// {
// Console.WriteLine($"Имя: {name}, Уровень: {level}, HP: {hp}");
// }
// }
//
// class Program
// {
// static void Main()
// {
//
// Pokemon pokemon1 = new Pokemon("Пикачу", 5, 100);
// Pokemon pokemon2 = new Pokemon("Сквиртл", 7, 90);
//
// Console.WriteLine("=== ПОКЕДЕКС ===");
// pokemon1.ShowInfo();
// pokemon2.ShowInfo();
// }
// }
class Pokemon
{
private string name;
private int level;
private int hp;
@@ -13,19 +67,39 @@ namespace lab06
public string Name
{
get { return name; }
set { name = value; }
set
{
if (string.IsNullOrWhiteSpace(value))
{
name = "Безымянный";
}
else
{
name = value;
}
}
}
public int Level
{
get { return level; }
set { level = value; }
set
{
if (value < 1) level = 1;
else if (value > 100) level = 100;
else level = value;
}
}
public int HP
{
get { return hp; }
set { hp = value; }
set
{
if (value < 0) hp = 0;
else if (value > 100) hp = 100;
else hp = value;
}
}
@@ -35,26 +109,39 @@ namespace lab06
this.level = level;
this.hp = hp;
}
public void ShowInfo()
public void TakeDamage(int damage)
{
Console.WriteLine($"Имя: {name}, Уровень: {level}, HP: {hp}");
if (hp > 0)
hp -= damage;
}
}
class Program
{
static void Main()
public void Heal()
{
hp = 100;
}
class Program
{
static void Main()
{
Console.WriteLine("=== ПРОВЕРКА ИНКАПСУЛЯЦИИ ===");
Pokemon pokemon1 = new Pokemon("Пикачу", 5, 100);
Pokemon pokemon2 = new Pokemon("Сквиртл", 7, 90);
// Создаём покемона через конструктор без параметров («яйцо»)
Pokemon pokemon = new Pokemon();
Console.WriteLine("=== ПОКЕДЕКС ===");
pokemon1.ShowInfo();
pokemon2.ShowInfo();
// Попытка присвоить некорректное значение HP (-50)
Console.Write("Попытка установить HP = -50... ");
pokemon.HP = -50;
Console.WriteLine($"Текущее HP: {pokemon.HP} (защита сработала)");
// Наносим урон
pokemon.TakeDamage(30);
Console.WriteLine($"Покемон получил 30 урона. HP: {pokemon.HP}");
// Лечим покемона
pokemon.Heal();
Console.WriteLine($"Покемон вылечился. HP: {pokemon.HP}");
}
}
}
}