BasicKnowledgeCSharp/LessonsAndTasks/Lesson 58 - ИНКАПСУЛЯЦИЯ, примеры/Program.cs
Dvurechensky 058c8f2679 1.0
Main
2024-10-05 09:59:53 +03:00

50 lines
1.2 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
/*
* Инкапсуляция
*/
class Gun
{
/// <summary>
/// Cостояние
/// </summary>
private bool isLoaded;
/// <summary>
/// Поведение
/// Инкапсуляция - скрываем состояние объекта - защищаем от внешнего мира
/// и даём возможность изменить состояние только с помощью этого же объекта
///
/// Cпособ правильно организовать поведение(работу объекта через методы)
/// </summary>
private void Reload()
{
Console.WriteLine("Заряжаю...");
isLoaded = true;
Console.WriteLine("Заряжено!");
}
public void Shot()
{
if (!isLoaded)
{
Console.WriteLine("Орудие не заряжено!");
Reload();
}
Console.WriteLine("Пыщ - Пыщ!\n");
isLoaded = false;
}
}
class Program
{
static void Main()
{
Gun gun = new Gun();
gun.Shot();
Console.ReadKey();
}
}