본문 바로가기

C#/수업 내용

대리자연습8

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(int id);

        //생성자
        public App()
        {
            //SCV클래스를 만든다.
            SCV scv = new SCV(100);
            //SCV클래스에 Build메서드를 만든다.
            scv.Build(this.BuildCompleteHandler);//4. 대리자 변수에 메서드 연결 (인수)
        }

        //3. 연결할 메서드 정의
        void BuildCompleteHandler(int id)
        {
            Console.WriteLine("complete! : {0}", id);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class SCV
    {
        private int idNum;

        //생성자
        public SCV(int idNum)
        {
            this.idNum = idNum;
        }

        //2.대리자 변수선언 (매개변수)
        public void Build(App.Del del)
        {
            del(this.idNum);  //5. 대리자 호출
        }
    }
}

 

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

람다식 연습 3 (Action) - 빵만들기  (0) 2021.12.24
람다식 연습2 (Action)  (0) 2021.12.24
대리자연습6 고블린 (오류수정)  (0) 2021.12.24
대리자연습6 고블린 (오류)  (0) 2021.12.24
대리자 연습 5  (0) 2021.12.24