////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

일단 구조체의 사용법을 좀 보자.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace ConsoleApplication1

{

    interface a

    {

    }


    struct Player : a

    {

        public string name;

        public int Att;

        public int Def;

        public int Hp;

        public int Dex;


        public Player(string name, int Att, int Def, int Hp, int Dex) -> 생성자를 만들었다면

        {

            this.name   = name;

            this.Att    = Att;

            this.Def    = Def;

            this.Hp     = Hp;

            this.Dex    = Dex; -> 내부에서 마지막 맴버까지 모든 맴버를 초기화 시켜줘야 한다. 그렇지 않으면 오류가 난다.

        }

        public void Show()

        {

            Console.WriteLine(name + "의 능력:");

            Console.WriteLine("공격력: " + Att + ", 방어력: " + Def + ", 체력:" + Hp + ", 민첩성: " + Dex);

        }

    }


    class Program

    {

        static void Main(string[] args)

        {

            Player Player1 = new Player("수깔", 94, 96, 84, 92);

            Player1.Show();


            Player Player2;

            Player2.name = "빨딴";

            Player2.Att = 100;

            Player2.Def = 84;

            Player2.Hp = 96;

            Player2.Dex = 86;

            Player2.Show();

        }

    }

}


c++에서 사용하는 구조체와 동일하게 이용이 가능하나 다음과 같은 차이가 생겼다.

1. 인터페이스를 제외하고는 상속이 불가능하다.(c++에서는 구조체도 클래스 상속이 가능했습니다. 아니 구조체와 클래스의 차이가 없다고 봐도 무방할 정도다.)

2. 디폴트 생성자를 지원하지 않는다.(생성자를 만들어 놓지 않으면 경고가 미친듯이 뜬다. 컴파일은 된다.)

3. 또한 initilizer를 지원하지 않는다. 무슨 말이냐면.


    struct Player

    {

        public int Att = 0; -> 구조체는 이런식으로 초기화가 불가능하다.

    }

4. 구조체는 명시적매개변수가 없는 명시적 생성자를 포함할수 없다.


    struct MyData

    {

        private int x, y;

        public MyData() -> 이런 녀석은 만들 수가 없다.

        {

            this.x = 0;

            this.y = 0;

        }

    }


5. 또한 내부의 static변수는 내부의 모든 맴버가 초기화 하기 전까지는 사용이 불가능하다.


    namespace ConsoleApplication1

    {

        interface a

        {

        }


        struct Player : a

        {

            public string name;

            public int Att;

            public int Def;

            public int Hp;

            static public int Dex;


            public Player(string name, int Att, int Def, int Hp, int Dex)

            {

                this.name = name;

                this.Att = Att;

                this.Def = Def;

                this.Hp = Hp;

            }

            public void Show()

            {

                Console.WriteLine(name + "의 능력:");

                Console.WriteLine("공격력: " + Att + ", 방어력: " + Def + ", 체력:" + Hp + ", 민첩성: " + Dex);

            }


            public void SetDex()

            {

                Dex = 100; -> 보면 알겠지만 static 맴버는 this라는 

            }

        }


        class Program

        {

            static void Main(string[] args)

            {

                Player Player1;

                //Player1.SetDex();

                Player1 = new Player("수깔", 94, 96, 84, 92);

                Player1.Show();

                Player1.SetDex(); -> 모든 변수가 초기화된 후에야 사용이 가능하다.


                Player Player2;

                Player2.name = "빨딴";

                Player2.Att = 100;

                Player2.Def = 84;

                Player2.Hp = 96; -> //Player2.Hp = 96; 초기화중 모든 맴버를 할당하지 않고 사용하려고 하면 오류가 뜬다. 할당되지 않은 지역변수를 사용했다는 오류다.

                                   // 그래서 내부적으로 맴버를 사용하지 않는 함수를 만들어서 시험해 사용해봤으나 마찬가지의 결과가 나왔다.

 // 즉 귀찮게 생각하지 말고 무조건 static맴버를 제외하고는 초기화 시켜라!

                Player2.Show();

                Player1.Show();

            }

        }

    }


7. 마지막으로 구조체는 값형의 데이터이다. 따로 주소가 할당되지 않기 때문에. 그냥 값형을 인자로 받는 함수에 넣을 경우 원본 데이터에는 아무런 영향이 가지 않는다.



'게임개발공부 > C#공부' 카테고리의 다른 글

변수가 사용됐다? 초기화의 순서는?  (0) 2013.12.30
인덱서  (0) 2013.12.30
추상 클래스와 인터페이스의 차이란?  (0) 2013.12.29
추상 클래스 봉인 클래스  (0) 2013.12.29
인터페이스  (0) 2013.12.29
Posted by JJOREG