BasicKnowledgeCSharp/LessonsAndTasks/Lesson 59 - Что такое конструктор класса, для чего, по умолчанию/Program.cs
Dvurechensky 058c8f2679 1.0
Main
2024-10-05 09:59:53 +03:00

86 lines
2.0 KiB
C#
Raw 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;
/*
* конструктор класса - это специальный метод класса, который вызывается при создании объекта такого класса (new)
*
* конструктор по умолчанию
*/
class Point
{
public Point(int x, int y)
{
_x = x;
_y = y;
}
private int _x;
private int _y;
public void Print()
{
Console.WriteLine($"X: {_x} \t Y: {_y}");
}
}
class Gun
{
/// <summary>
/// конструктор без параметров
///
/// ctor - конструктор по умолчанию
/// конструктор - тоже метод класса но только специальный, всегда public
/// можно выставить что сделать по умолчанию
/// </summary>
public Gun()
{
_isLoaded = true;
}
/// <summary>
/// конструктор с параметрами
/// </summary>
/// <param name="isLoaded">#</param>
public Gun(bool isLoaded)
{
this._isLoaded = isLoaded;
}
private bool _isLoaded;
/// <summary>
/// Поведение
/// </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()
{
//new - конструктор по умолчанию (спец метод для создания объекта класса)
var gun = new Gun(isLoaded: true);
gun.Shot();
Point point = new Point(1, 3);
point.Print();
Console.ReadKey();
}
}