Unity/수업내용(C#)
[3.22] delegate chain (Multicast delegate)
ljw4104
2021. 3. 23. 17:12
using System;
using System.Collections.Generic;
namespace Study09
{
public class CarDriver
{
public CarDriver()
{
}
public static void GoLeft()
{
Console.WriteLine("GoLeft");
}
public static void GoForward()
{
Console.WriteLine("GoForward");
}
}
public class App
{
//대리자 선언
public delegate void GoHome();
public delegate void Say();
public App()
{
//대리자 변수 선언 및 초기화
GoHome go = new GoHome(CarDriver.GoLeft);
go += new GoHome(CarDriver.GoForward);
go -= new GoHome(CarDriver.GoLeft);
go();
Say say = new Say(Hi);
say += new Say(Hello);
say();
say -= new Say(Hello);
say();
}
private void Hello()
{
Console.WriteLine("Hello");
}
private void Hi()
{
Console.WriteLine("Hi");
}
}
}
using System;
namespace Study09
{
public class App
{
public delegate void SendMessage(string message);
public App()
{
SendMessage del1 = this.Hello;
SendMessage del2 = this.Hi;
del1("홍길동");
del2("임꺽정");
Console.WriteLine("===================================");
//멀티캐스트를 통한 초기화
SendMessage del = del1 + del2;
del("장길산");
Console.WriteLine("===================================");
SendMessage del3 = (string name) =>
{
Console.WriteLine("안녕하세요, {0}님", name);
};
del += del3;
del("고길동");
Console.WriteLine("===================================");
del -= del2;
del("둘리");
Console.WriteLine("===================================");
del -= del1;
del("도우너");
}
public void Hello(string name)
{
Console.WriteLine("Hello {0}", name);
}
public void Hi(string name)
{
Console.WriteLine("Hi {0}", name);
}
}
}
using System;
namespace Study09
{
public class App
{
public delegate void ThereIsFire(string location);
public App()
{
ThereIsFire fire = Call119;
fire += ShotOut;
fire += Escape;
fire("우리집");
}
public void Call119(string location)
{
Console.WriteLine("소방서죠? 불났어요! 주소는 {0}", location);
}
public void ShotOut(string location)
{
Console.WriteLine("피하세요! {0}에 불났어요!", location);
}
public void Escape(string location)
{
Console.WriteLine("{0}에서 나갑니다!", location);
}
}
}