위 사진을 수정
----------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld1
{
class App
{
public Goblin goblin;
//생성자
public App()
{
this.goblin = new Goblin();
//3.대리자 인스턴스화
goblin.onAttackComplete = this.OnattackComplete;
goblin.ChangeState(Goblin.eState.Idle);
goblin.ChangeState(Goblin.eState.Attack);
Goblin.eState state = goblin.GetState();
Console.WriteLine("state: {0}", state); //Idle
}
//2.대리자에 연결할 메서드 정의
public void OnattackComplete()
{
this.goblin.ChangeState(Goblin.eState.Idle);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld1
{
class Goblin
{
public enum eState
{
Idle, Attack
}
//1. 대리자 선언
public delegate void Del();
//대리자 변수 정의
public Del onAttackComplete;
private eState State;
//생성자
public Goblin()
{
}
public void ChangeState(eState state)
{
this.State = state;
Console.WriteLine(this.State);
if(this.State == eState.Attack)
{
this.onAttackComplete();
}
}
public eState GetState()
{
return this.State;
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld1
{
class App
{
//생성자
public App()
{
SCV scv = new SCV(100);
//3.대리자 인스턴스화
scv.Build(this.BuildconpleteHandler);
}
//3.대리자에 연결할 메서드 정의
void BuildconpleteHandler(int id)
{
Console.WriteLine("conplete! : {0}", id);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld1
{
class SCV
{
//1.대리자 정의
public delegate void Del(int id);
public int id;
public SCV(int id)
{
this.id = id;
}
public void Build(Del del)
{
//4.대리자 호출
del(this.id);
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------
'c# > 수업 내용' 카테고리의 다른 글
대리자 익명 함수 정의 (*꼭 알아야 할 것*) (0) | 2021.09.07 |
---|---|
멀티캐스트 대리자 정의 (0) | 2021.09.07 |
대리자 수정하기 연습 (0) | 2021.09.06 |
대리자 (DalyGate) 정의 (0) | 2021.09.06 |
예외 및 예외 처리 (try-catch-finally) (0) | 2021.09.06 |