C#프로그래밍

상속

다모아 2023. 7. 24. 12:24

App 클래스

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnDotnet
{
    internal class App
    {
        //생성자 메서드
        public App()
        {
            Marine marine = new Marine(); //호출은 부모가 먼저되고 
            int hp = marine.GetHp();
            Console.WriteLine(hp);
        }
    }
}

 

TerranUnit [부모]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnDotnet
{
    internal class TerranUnit
    {
        //멤버변수
        //protected : 파생 클래스까지 접급할 수 있음
        protected int hp = 10; //모든 테란 유닛은 생명력을 가지고있다.

        //생성자메서드
        public TerranUnit()
        {
            Console.WriteLine("TerranUnit클래스의 생성자 호출됨");
            Console.WriteLine(this); //클래스의 현재 인스턴스
            Console.WriteLine(this.hp);
        }
        //멤버메서드
        public int GetHp()
        {
            return this.hp; //marine의 hp
        }
    }
}

 

marine 클래스

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnDotnet
{
    internal class Marine : TerranUnit
    {
        //멤버변수

        //생성자메서드
        public Marine()
        {
            Console.WriteLine("Marine클래스 생성자 호출됨");
            Console.WriteLine(this.hp);
        }
        //멤버메서드
    }
}

 

'C#프로그래밍' 카테고리의 다른 글

생성자 체이닝  (0) 2023.07.24
virtual, override, overload, base  (0) 2023.07.24
저글링, 마린, 메딕  (0) 2023.07.24
저글링 vs 마린  (0) 2023.07.21
플레이어, 몬스터, 무기[출력만, 입력포함]  (0) 2023.07.21