본문 바로가기
C# 언어/C# 기초 문법

[C#] 객체지향

by 후야- 2024. 3. 22.
using System.Numerics;
using System.Security.Cryptography.X509Certificates;

namespace CShop
{
    class Player  // 상속해주는 부모 / 기반
    {
        // static 오로지 1개만 존재
        static public int counter = 1;

        // public : 공공 없으면 안에서만 사용 가능
        public int id;
        public int hp;
        public int attack;

        public Player()
        {
            Console.WriteLine("플레이어 호출");
        }
    }

    class Archer : Player
    {

    }

    class Knight : Player    // 자식 혹은 파생 클래스
    {
        Knight()
        {
            Console.WriteLine( "나이트 생성자 호출");
        }

        /*// 생성자 , 아무것도 입력 x 반환 x
        public Knight()
        {
            id = counter;
            counter++;

            hp = 100;
            attack = 15;
            Console.WriteLine("생성자 호출!");
        }
        */
        static public Knight CreateKnight()
        {
            Knight knight = new Knight();
            knight.hp = 1000;
            knight.attack = 50;
            return knight;
        }
        // this
        public Knight(int hp) : this()
        {
            this.hp = hp;
            Console.WriteLine("int 생성자 호출");
        }

        public Knight(int hp, int attack) : this(hp)
        {
            this.hp = hp;
            this.attack = attack;
            Console.WriteLine("int 생성자 호출");
        }

        public Knight Clone()
        {
            Knight knight = new Knight();
            knight.hp = hp;
            knight.attack = attack;
            return knight;
        }

        public void Move()
        {
            Console.WriteLine("나이트 이동");
        }

        public void Attack()
        {
            Console.WriteLine("나이트 공격");
        }
    }


    class Program
    {
        static void Main(string[] args)
        {

            Knight knight = Knight.CreateKnight();


        }

    }
}

'C# 언어 > C# 기초 문법' 카테고리의 다른 글

[C#] 객체지향 직업선택  (0) 2024.03.30
직업 선택  (0) 2024.03.19
[C#] 함수  (0) 2024.03.19
[C#] 열거형  (0) 2024.03.19
[C#] 반복문 / break / continue  (0) 2024.03.19