본문 바로가기
C#

static 이란

by Mostlove 2023. 7. 5.
728x90
반응형

using static CSharp.Program;

namespace CSharp
{
    class Knight
    {
        static public int counter;//오로지 1개만 존재!

        public int id;
        public int hp;
        public int attack;
        static public void Test()
        {
            counter++;//static 함수 내에서는 static만 값변경 가능
        }
        static public Knight CreateKnight()
        {
            Knight knight = new Knight();
            knight.hp = 100;
            knight.attack = 1;
            return knight;
        }
        public Knight()
        {
            id = counter;
            counter++;
            hp = 100;
            attack = 10;
            Console.WriteLine("생성자 호출!");
        }
        public Knight Clone()
        {
            Knight knight = new Knight();
            knight.hp = hp;
            knight.attack = attack;
            return knight;
        }
        public void Move()
        {
            Console.WriteLine("Knight Move");
        }
        public void Attack()
        {
            Console.WriteLine("Knight Attack");

        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            int b = Knight.counter;//static 붙은 함수 또는 변수는 다른 클래스에서도 사용가능
            Knight knight = Knight.CreateKnight();
            knight.Move();
            Console.WriteLine();
            Random rand = new Random();
            rand.Next(0, 2);
        }
    }
}

반응형

'C#' 카테고리의 다른 글

은닉성  (0) 2023.07.05
상속  (0) 2023.07.05
생성자  (0) 2023.07.05
스택과 힙  (0) 2023.07.05
복사(값)와 참조  (0) 2023.07.04