C#/수업 내용

[C#] Delegate 대리자

JSH1 2021. 9. 6. 16:08
        //1. 대리자 정의
        delegate void Del();

        public App()
        {
            //3. 대리자 변수에 메서드를 연결한다
            Del handler = this.SayHello;

            //4. 대리자 사용
            handler();
        }

        //2. 대리자에 연결할 메서드 정의
        void SayHello()
        {
            Console.WriteLine("Hello Wrold!");
        }


        //1. 대리자 정의
        delegate void Del(string message);

        public App()
        {
            //3. 대리자 변수에 메서드를 연결한다
            //대리자 인스턴스화 (메서드 연결)
            Del handler = new Del(Say);
            handler = Say2;

            //4. 대리자 사용
            handler("Hello Wrold!");
        }

        //2. 대리자에 연결할 메서드 정의
        void Say(string message)
        {
            Console.WriteLine(message);
        }

        void Say2(string message)
        {
            Console.WriteLine("Say2: {0}", message);
        }


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

        public App()
        {
            //3. 대리자 인스턴스화 (메서드 연결)            
            Del del = Sum;

            //4. 대리자 사용
            int sum = del(1, 2);
            Console.WriteLine(sum);
        }

        //2. 대리자에 연결할 메서드 정의
        int Sum(int a, int b)
        {
            return a + b;
        }


        //1. 대리자 정의
        delegate void Del();

        public App()
        {
            //3. 대리자 인스턴스화 (메서드 연결)            
            Del del = Die;

            //콜백 메서드로써의 대리자 사용
            DieSCV(del);
        }

        void DieSCV(Del del)
        {
            //4. 대리자 호출
            del();
        }

        //2. 대리자에 연결할 메서드 정의
        void Die()
        {
            Console.WriteLine("Die");
        }


    class App
    {
        //1. 대리자 정의
        public delegate void Del();

        public App()
        {
            //3. 대리자 인스턴스화 (메서드 연결)            
            Del del = Die;

            SCV scv = new SCV();
            //SCV가 죽었다면 del 실행
            scv.AddDieHandler(del);
            scv.Die();
        }

        void DieSCV(Del del)
        {
            //4. 대리자 호출
            del();
        }

        //2. 대리자에 연결할 메서드 정의
        void Die()
        {
            Console.WriteLine("이펙트를 생성한다.");
        }
    }
    class SCV
    {
        private App.Del del;

        public SCV()
        {

        }

        public void AddDieHandler(App.Del del)
        {
            this.del = del;
        }

        public void Die()
        {
            this.del();
        }
    }


    class App
    {
        public delegate void Del(int id);

        public App()
        {
            Del del = this.MoveComplete;

            SCV scv = new SCV(100);
            scv.Move(del);
        }

        void MoveComplete(int id)
        {
            Console.WriteLine("SCV({0}) move complete", id);
        }
    }
    class SCV
    {
        int id;

        public SCV(int id)
        {
            this.id = id;
        }

        public void Move(App.Del del)
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("이동중...");
            }
            del(this.id);
        }
    }


    class App
    {

        public App()
        {
            Button btn = new Button();
            btn.onclick = this.OnClick;
            btn.Click();
        }

        void OnClick()
        {
            Console.WriteLine("Click");
        }
    }
    class Button
    {
        //1. 대리자 선언
        public delegate void Del();

        //3-1. 대리자 변수 선언
        public Del onclick;

        public Button()
        {

        }

        public void Click()
        {
        	//4. 대리자 호출
            onclick();
        }
    }


    class App
    {

        public App()
        {
            Blacksmith blacksmith = new Blacksmith();
            blacksmith.CreateWeapon("장검", CreateWeaponHandler);
        }

        public void CreateWeaponHandler(Weapon weapon)
        {
            Console.WriteLine("created weapon: {0}", weapon.name);
        }
    }
    class Blacksmith
    {
        public delegate void Del(Weapon weapon);

        public void CreateWeapon(string name, Del callback)
        {
            Weapon weapon = new Weapon(name);
            callback(weapon);
        }
    }

'C# > 수업 내용' 카테고리의 다른 글

[C#] Action 람다식 연습문제  (0) 2021.09.07
[C#] delegate callback  (0) 2021.09.06
[C#] try/catch 예외 및 예외 처리  (0) 2021.09.06
[C#] 2021-09-06 Dictionary 인벤토리  (0) 2021.09.06
[C#] 2021-09-03 Dictionary 연습  (1) 2021.09.03