using System;
using System.Numerics;
namespace Rookiss_CSharp
{
class Program
{
class Knight
{
static public int counter; // static : 각 인스턴스에 종속적인게 아니라, 오로지 1개만 존재 => static은 메모리 공유해서 사용됨
public int id;
public int hp;
public int attack;
static public void Test() // 클래스에 종속된 것이라 유일성 보장(공용함수)
{
// 내부에서는 각 인스턴스의 필드에 접근 불가
// static 변수만 사용가능
counter++;
}
public static Knight CreateKnight()
{
// static 함수 내부에서 할당된 인스턴스인 경우 필드 접근 가능
Knight knight = new Knight();
knight.hp = 100;
knight.attack = 1;
return knight;
}
public Knight()
{
id = counter;
counter++;
hp = 100;
attack = 10;
Console.WriteLine("생성자 호출!");
}
public void Move()
{
Console.WriteLine("Knight Move");
}
public void Attack()
{
Console.WriteLine("Knight Attack");
}
}
struct Mage
{
public int hp;
public int attack;
}
static void Main(string[] args)
{
Knight knight = Knight.CreateKnight(); // static
knight.Move(); // 일반
// Console.WriteLine(); // static
}
}
}
참조
static의 정체 | [C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part1: C# 기초 프로그래밍 입문
static의 정체
www.inflearn.com
39강 static의 정체