BasicKnowledgeCSharp/LessonsAndTasks/Lesson 63 - Статические поля класса, ключевое слово static/Program.cs
Dvurechensky 058c8f2679 1.0
Main
2024-10-05 09:59:53 +03:00

41 lines
935 B
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;
/*
* Cлючевое слово static
*
* Статические поля класса (к полям, методам, свойствам, классам, конструкторам класса)
*/
class MyClass
{
public int a;
/// <summary>
/// static - память которая выделяется доступна для всех объектов myclass
/// общая для всех экземпляров класса
/// </summary>
public static int b;
private static int g;
public void SetG(int g) => MyClass.g = g;
public void PrintG() => Console.WriteLine(g);
}
class Program
{
static void Main()
{
MyClass.b = 4;
MyClass myClass1 = new MyClass();
myClass1.a = 22;
MyClass myClass2 = new MyClass();
myClass2.a = 44;
myClass1.SetG(1);
myClass2.PrintG();
Console.ReadKey();
}
}