BasicKnowledgeCSharp/LessonsAndTasks/Lesson 57 - Модификаторы доступа, public, protected, private/Program.cs
Dvurechensky 058c8f2679 1.0
Main
2024-10-05 09:59:53 +03:00

57 lines
1.2 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;
using System.Reflection;
/*
* модификаторы доступа public и private(используются для реализации инкапсуляции), для членов класса
*/
class Point
{
/// <summary>
/// *по умолчанию private
/// </summary>
int z = 3;
public int x = 1;
private int y = 5;
private void PrintX()
{
Console.WriteLine($"X: {x}");
}
public void PrintY()
{
Console.WriteLine($"Y: {y}");
}
public void PrintPoint()
{
PrintX();
PrintY();
}
}
class Program
{
static void Main()
{
Point point = new Point();
point.PrintY();
point.x = 2;
point.PrintPoint();
// проверим то, что по умолчанию стоит private у поля используя рефлексию
var typeInfo = typeof(Point).
GetFields(BindingFlags.Instance
| BindingFlags.NonPublic
| BindingFlags.Public);
foreach (var item in typeInfo)
{
Console.WriteLine($"{item.Name}\t isPrivate: {item.IsPrivate}\t isPublic: {item.IsPublic}");
}
Console.ReadKey();
}
}