C#/수업 내용

대리자 연습 2

hyunjin-dev-log 2021. 12. 24. 22:27

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

namespace HelloWorld
{
    class App
    {
        //1 대리자 선언
        public delegate void Del();

        //생성자
        public App()
        {
            Button btn = new Button();
            //3-2. 대리자 초기화
            btn.onclick = this.OnClick;
            btn.Click();
        }

        //2 대리자에 연결될 메서드
        void OnClick()
        {
            Console.WriteLine("Click");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Button
    {
        //3-1 변수 선언
        public App.Del onclick;

        //생성자
        public Button()
        {

        }


        public void Click()
        {
            this.onclick();
        }
    }
}