06.상속성 — 웹툰PD 지망생 : 정민재
06.상속성
●/섹션 4. 객체지향 여행

06.상속성

728x90

 

using System;

namespace Csharp
{
    //OOP(객체지향 3대장) = (은닉성 / 상속성 / 다형성)


    class Mage
    {       
        static public int counter = 1;

        public int id;
        public int hp;
        public int attack;
    }

    class Archer
    {        
        static public int counter = 1;

        public int id;
        public int hp;
        public int attack;
    }

    class Knight
    {        
        static public int counter = 1;

        public int id;
        public int hp;
        public int attack;
    }
}

 

각 직업별로 hp변수, attack변수를 선언하는 것은 대단히 비효율적이다.

 

몬스터의 경우, 몇 백마리가 넘어가는데, 이를 다 선언해주는 것 또한 굉장히 비효율적.

 

그러므로 이 때 상속을 쓴다.

 

using System;

namespace Csharp
{
    //OOP(객체지향 3대장) = (은닉성 / 상속성 / 다형성)


    class Player            //부모클래스 or 기반클래스
    {
        //static은 변수뿐만 아니라 나중에 함수에도 붙일 수 있다!
        static public int counter = 1;    //오로지 1개만 존재

        public int id;
        public int hp;
        public int attack;
    }
    class Mage : Player     //자식클래스 or 파생클래스
    {
        //상속 받음      
    }

    class Archer : Player   //자식클래스 or 파생클래스
    {
        //상속 받음
    }

    class Knight : Player   //자식클래스 or 파생클래스
    {
        //상속 받음
    }
}

 

 

디버그 순서 - 중단점 잡고 F11 연타

using System;

namespace Csharp
{ 
    class Player
    {
        public Player()                                  //4
        {                                                //5
            Console.WriteLine("Player 생성자 호출!");    //6
        }                                                //7   <----- 디버그창의 첫 번째 줄 출력 : Player 생성자 호출!  
    }
    class Mage : Player
    {        
        public Mage()                                    //3  //8
        {                                                //9
            Console.WriteLine("Mage 생성자 호출!");      //10
        }                                                //11  <----- 디버그창의 두 번째 줄 출력 : Mage 생성자 호출!
    }
    
    class Program
    {
        static void Main(string[] args)
        {                                                //1
            Mage mage = new Mage();                      //2  //12
        }                                                //13(끝)
    }
}

 

디버그 순서 - 매개변수

using System;

namespace Csharp
{
    class Player
    {
        static public int counter = 1;
        public int id;
        public int hp;
        public int attack;
        public Player()
        {
            Console.WriteLine("Player 생성자 호출!");
        }

        public Player(int hp)                            //4
        {                                                //5
            this.hp = hp;                                //6
            Console.WriteLine("Player hp 생성자 호출!"); //7
        }                                                //8  <----- 디버그창에 출력 (Player hp 생성자 호출!)
    }
    class Mage : Player
    {
        //:Player(100)는 먹히지 않지만 base로 하면 먹힌다.
        public Mage() : base(100)                        //3  //9
        {                                                //10
            Console.WriteLine("Mage 생성자 호출!");      //11
        }                                                //12 <----- 디버그창에 출력 (Mage 생성자 호출!)
    }


class Program
    {
        static void Main(string[] args)
        {                                                //1
            Mage mage = new Mage();                      //2  //13
        }                                                //14(끝)
    }
}

this와 base의 차이

using System;

namespace Csharp
{
    class Player
    {
        static public int counter = 1;
        public int id;
        public int hp;
        public int attack;
        public Player()
        {
            Console.WriteLine("Player 생성자 호출!");
        }
    }
    class Mage : Player
    {
        int c;
        
        public Mage() : base(100)   // :Player(100)는 먹히지 않지만 base로 하면 먹힌다.
        {
            this.c = 10;        //여기서 this는 Player()가 아니라 Mage()를 가리킨다. (this = 내가 속해있는 클래스)
            base.hp = 100;      //여기서 base는 Mage()가 아니라 Player()를 가리킨다. (base = 내가 속해있는 클래스가 아니라, 내가 속해있는 클래스의 부모님)
                                //즉, (base = 부모님)(즉, 자식이 부모를 부를 때 base ~ 라고 부른다)
            
            Console.WriteLine("Mage 생성자 호출!");
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Mage mage = new Mage();
        }
    }
}

 

부모 클래스에게 함수도 상속 받기

using System;

namespace Csharp
{
    class Player
    {
        static public int counter = 1;
        public int id;
        public int hp;
        public int attack;

        public void Move()
        {
            Console.WriteLine("Move");
        }
    }

    class Mage : Player
    {

    }

    class Program
    {
        static void Main(string[] args)
        {
            Mage mage = new Mage();

            //부모에게 함수도 상속 받을 수 있다.
            mage.Move();
        }
    }
}

728x90

' > 섹션 4. 객체지향 여행' 카테고리의 다른 글

09.다형성  (0) 2021.07.12
07.은닉성  (0) 2021.07.12
05.static (일반함수 VS static(정적)함수)  (0) 2021.07.12
04.생성자  (0) 2021.07.11
03.스택과 힙  (0) 2021.07.11