본문 바로가기
KDT/C# 프로그래밍

23/07/27 과제(복습)

by 잰쟁 2023. 7. 27.
728x90

※복습할 것 

- 대리자 메서드 연습 (무명메서드, 람다, Action 중심으로)

 

▶대리자(delegate)

- 기본 형식

ex) (public) delegate int MyDelegate(int a, int b)

 

-구현 과정
1) 메서드 정의
2) 대리자 형식 정의
3) 대리자 초기화 (대리자 인스턴스 생성 및 메서드 연결)
4) 대리자 호출 

 

▶대리자(무명메서드)

- 기본 형식

ex)  MyDelegate del = delegate (int a, int b) {

              return a + b;

        };

 

-구현 과정

1) 대리자 형식 정의

2) 대리자 인스턴스화

3) 대리자 호출

 

대리자 - 무명메서드

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



namespace Starcraft
{
    public class App
    {

        //1. 대리자 형식 정의
        delegate void MyDelegate();

        //생성자
        public App()
        {
            //2. 대리자 인스턴스화 
            MyDelegate myDel = delegate () {
                Console.WriteLine("안녕하세요");
            };

            //3. 대리자 호출
            myDel();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;



namespace Starcraft
{
    public class App
    {

        //1. 대리자 형식 정의
        delegate int MyDel(int a, int b);

        //생성자
        public App()
        {
            //2. 대리자 인스턴스화 
            MyDel del = delegate (int a,int b) {
                return a + b;
            };

            //3. 대리자 호출
            int sum = del(3, 4);
            Console.WriteLine(sum);
        }
    }
}

 

▶람다

- 익명 함수를 만들기 위해 사용
- 람다 선언 연산자: =>

- 기본 형식

ex) MyDelegate del = (int a, int b) => {

              return a + b;

        };

 

-구현 과정

1) 대리자 형식 정의

2) 대리자 인스턴스화

3) 대리자 호출

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



namespace Starcraft
{
    public class App
    {
        //1. 대리자 형식 정의
        delegate int MyDel(int a, int b);

        //생성자
        public App()
        {
            //2. 대리자 인스턴스화
            MyDel del = (a, b) => {
                return a + b;
            };

            //3. 대리자 호출
            del(2, 3);
            Console.WriteLine(del(2, 3));
        }
    }
}

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



namespace Starcraft
{
    public class App
    {

        //생성자
        public App()
        {
            //대리자 변수 정의
            Action action;

            //대리자 인스턴스화 (메서드 연결)
            action = this.SayHello;

            //람다
            action = () => {
                Console.WriteLine("Hello~~");
            };

            //대리자 호출
            action();
          
        }
        public void SayHello()
        {
            Console.WriteLine("안녕하세요.");
        }
    }
}

 

(Action 연습 내용 1)

영웅이 App에서 진짜 이동을 완료하게 하고픔

"영웅 진짜 이동 완료!!" 출력

 

Hero 클래스

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

namespace Starcraft
{
    public class Hero
    {

        //생성자
        public Hero()
        {
            Console.WriteLine("영웅이 생성되었습니다.");
        }

        //이동 메서드
        public void Move(Action callback)
        {
            Console.WriteLine("이동중...");
            Console.WriteLine("이동중...");
            Console.WriteLine("이동중...");
            Console.WriteLine("이동을 완료했습니다.");

            callback();
        }
    }
}

App 클래스

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



namespace Starcraft
{
    public class App
    {
        
        //생성자
        public App()
        {
            //대리자 초기화 (대리자 인스턴스화/메서드 연결)
            Action moveComplete = () => {
                Console.WriteLine("영웅 진짜 이동완료!!");
            };

            //영웅 생성
            Hero hero = new Hero();
            hero.Move(moveComplete); //대리자 인스턴스를 인수로 전달
        }
    }
}

(Action 연습내용 2)

Hero 클래스

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

namespace Starcraft
{
    public class Hero
    {
        //맴버
        public Action moveComplete; //대리자 변수 정의

        //생성자
        public Hero()
        {
            Console.WriteLine("영웅이 생성되었습니다.");
        }

        //이동 메서드
        public void Move()
        {
            Console.WriteLine("이동중...");
            Console.WriteLine("이동중...");
            Console.WriteLine("이동중...");
            Console.WriteLine("이동을 완료했습니다.");

            this.moveComplete();
        }
    }
}

App 클래스

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


namespace Starcraft
{
    public class App
    {
      
        //생성자
        public App()
        {
            //영웅 생성
            Hero hero = new Hero();
            hero.moveComplete = ()=>{
                Console.WriteLine("영웅 진짜 이동 완료!!");
            };
            hero.Move();
        }
    }
}

(Action 연습내용 3)

Hero 클래스

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

namespace Starcraft
{
    public class Hero
    {
      
        //생성자
        public Hero()
        {
            Console.WriteLine("영웅이 생성되었습니다.");
        }

        //이동 메서드
        public void Move(Action callback)
        {
            Console.WriteLine("이동중...");
            Console.WriteLine("이동중...");
            Console.WriteLine("이동중...");
            Console.WriteLine("이동을 완료했습니다.");

            callback();
        }
    }
}

App 클래스

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


namespace Starcraft
{
    public class App
    {
      
        //생성자
        public App()
        {
            //영웅 생성
            Hero hero = new Hero();
            hero.Move(() => {
                Console.WriteLine("영웅 진짜 이동 완료!!");
            });
        }
    }
}

3가지 방법 모두 결과는 같음!

 

대리자 연습 1

App 클래스

using System;
using System.Collections.Generic;
using System.Collections;


namespace Starcraft
{
    public class App
    {
        //생성자
        public App()
        {
            Hero hero = new Hero();
            hero.HitDamage(3,(hp,maxHp) => {
                Console.WriteLine("영웅 실제 체력: [{0}/{1}]", hp, maxHp);
            });
        }
    }
}

Hero 클래스

using System;
using System.Collections.Generic;


namespace Starcraft
{
    public class Hero
    {
        //맴버
        int hp;
        int maxHp;

        //생성자
        public Hero()
        {
            Console.WriteLine("영웅이 생성되었습니다.");
            this.maxHp = 10;
            this.hp = maxHp;
        }

        //hp 변동 메서드
        public void HitDamage(int damage,Action<int,int> callback)
        {
            Console.WriteLine("Hp 닳는 중... (-{0})",damage);
            this.hp -= damage;

            callback(this.hp, this.maxHp);
        }
    }
}

 

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

23/07/30 과제(미션)  (1) 2023.07.30
23/07/28 게임 세팅  (0) 2023.07.28
23/07/27 아이템 정보 저장하기  (0) 2023.07.27
23/07/27 대리자 연습  (0) 2023.07.27
23/07/27 Action 대리자  (0) 2023.07.27