C#프로그래밍

대리자(Action) 연습

노재두내 2023. 7. 27. 12:52
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnDotnet
{
    public class Hero
    {
        public Action onAttackComplete;
        
        public Hero()
        {
            Console.WriteLine("영웅생성");
        }

        public void Attack()
        {
            Console.WriteLine("영웅이 공격했습니다");

            this.onAttackComplete();
        }
        public void ItemGet(Action callback)
        {
            Console.WriteLine("영웅이 아이템을 얻었습니다");
            callback();
        }

        public void Move(Action callback)
        {
            Console.WriteLine("이동중 ..");
            Console.WriteLine("이동중 ..");
            Console.WriteLine("이동완료 ..");

            callback();
        }
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;

namespace LearnDotnet
{
    public class App
    {
        
        //생성자
        public App()
        {
            Hero hero = new Hero();
            Action moveComplete = () => {
                Console.WriteLine("영웅 이동완료");
            };
            hero.onAttackComplete = () => {
                Console.WriteLine("영웅이 공격을 완료했습니다");
            };
            hero.ItemGet(() =>
            {
                Console.WriteLine("영웅이 아이템을 얻기를 완료했습니다");
            });
            hero.Move(moveComplete);
            hero.Attack();
        }
       
       
    }

}