Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Programming
- 기초
- dfs
- C#
- Unity
- 문제풀이
- UE5
- loop
- 시작해요 언리얼 2022
- github
- Tutorial
- python
- 재귀
- parameter
- DP
- 오류
- guide
- String
- 백준
- W3Schools
- Basic
- Class
- c++
- 파이썬
- Algorithm
- w3school
- Material
- dynamic
- Unreal Engine 5
- 프로그래밍
Archives
- Today
- Total
행복한 개구리
C# 21.03.22.수업내용 본문
★대리자 (deligate)
대리자는 특정 매개 변수 목록 및 반환 형식이 있는 메서드에 대한 참조를 나타내는 형식입니다. 대리자를 인스턴스화하면 모든 메서드가 있는 인스턴스를 호환되는 시그니처 및 반환 형식에 연결할 수 있습니다. 대리자 인스턴스를 통해 메서드를 호출할 수 있습니다.
대리자는 메서드를 다른 메서드에 인수로 전달하는 데 사용됩니다. 이벤트 처리기는 대리자를 통해 호출되는 메서드라고 할 수 있습니다
액세스 가능한 클래스 또는 대리자 형식과 일치하는 구조의 모든 메서드는 대리자에 할당할 수 있습니다.
================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study004
{
class App
{
//1. 델리게이트 선언
public delegate void Del(string message);
//생성자
public App()
{
Console.WriteLine("App");
//3. 델리게이트 인스턴스화 (델리게이트에 메서드 연결)
Del handler = this.DelegateMethod;
//4. 델리게이트 호출
handler("Hello World!");
MethodWithCallback(handler);
}
//2. 델리게이트 메서드 정의
public void DelegateMethod(string message)
{
Console.WriteLine(message);
}
//콜백 메서드 정의
public void MethodWithCallback(Del callback)
{
//4.델리게이트 호출
callback("Hello World");
}
}
}
================================================
델리게이트 연습 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class App
{
public App()
{
Console.WriteLine("App");
Button btn = new Button();
btn.onClick = this.OnClick;
btn.Click();
}
public void OnClick()
{
Console.WriteLine("설정창을 엽니다.");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class Button
{
public delegate void DelOnClick();
public DelOnClick onClick;
public Button()
{
}
public void Click()
{
Console.WriteLine("클릭");
this.onClick();
}
}
}
================================================
델리게이트 연습2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class App
{
public App()
{
Console.WriteLine("App");
Building building = new Building();
building.onComplete = this.OnComplete;
building.Build();
}
private void OnComplete()
{
Console.WriteLine("건설이 완료되었습니다.");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class Building
{
public delegate void DelOnComplete();
public DelOnComplete onComplete;
public Building()
{
}
public void Build()
{
Console.WriteLine("건설을 시작합니다.");
this.onComplete();
}
}
}
================================================
델리게이트 3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class App
{
public App()
{
Console.WriteLine("App");
FileManager fm = new FileManager();
fm.onOpenComplete = this.OnOpenComplete;
fm.Open("C:\test.txt");
}
private void OnOpenComplete()
{
Console.WriteLine("파일을 열었습니다.");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class FileManager
{
public delegate void DelOnOpenComplete();
public DelOnOpenComplete onOpenComplete;
public FileManager()
{
}
public void Open()
{
Console.WriteLine("파일을 엽니다.");
}
}
}
================================================
델리게이트 4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class App
{
public App()
{
Console.WriteLine("App");
SceneManager sm = new SceneManager();
sm.onLoaded = this.OnLoaded;
sm.LoadScene("Lobby");
}
private void OnLoaded()
{
Console.WriteLine("On Loaded");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class SceneManager
{
public delegate void DelOnLoaded();
public DelOnLoaded onLoaded;
public SceneManager()
{
}
public void LoadScene(string name)
{
Console.WriteLine("{0} 불러오기", name);
this.onLoaded();
}
}
}
================================================
델리게이트 5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class App
{
public App()
{
Console.WriteLine("App");
GameLauncher launcher = new GameLauncher();
launcher.onEndGame = this.OnEndGame;
launcher.StartGame();
}
private void OnEndGame()
{
Console.WriteLine("게임을 종료합니다.");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class GameLauncher
{
public delegate void DelOnEndGame();
public DelOnEndGame onEndGame;
public GameLauncher()
{
}
public void StartGame()
{
Console.WriteLine("게임을 시작합니다.");
this.onEndGame();
}
}
}
================================================
매개변수가 존재하는 델리게이트
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class App
{
public App()
{
Console.WriteLine("App");
Marine marine = new Marine(1);
marine.moveComplete = this.MoveComplete;
marine.Move();
}
private void MoveComplete(int id)
{
Console.WriteLine("Marine({0})가 이동 완료", id);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
public delegate void DelMoveComplete(int id);
class Marine
{
public int Id
{
get; private set;
}
public DelMoveComplete moveComplete;
public Marine(int id)
{
}
public void Move()
{
Console.WriteLine("[Marine] 마린 이동합니다.");
this.moveComplete(this.Id);
}
}
}
================================================
System.Action
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class App
{
public App()
{
Console.WriteLine("App");
Marine marine = new Marine(143643);
marine.moveComplete = this.MoveComplete;
marine.Move();
}
private void MoveComplete()
{
Console.WriteLine("MoveComplete");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class Marine
{
public int Id
{
get; private set;
}
public Action moveComplete;
public Marine(int id)
{
this.Id = id;
}
public void Move()
{
Console.WriteLine("[Marine] 마린 이동합니다.");
this.moveComplete();
}
}
}
================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class App
{
public App()
{
Console.WriteLine("App");
Zealot zealot = new Zealot(0, 0);
zealot.onMoveComplete = this.OnMoveComplete;
zealot.onDie = this.OnDie;
zealot.Move(1, 0);
zealot.Die();
}
private void OnMoveComplete(float x, float y)
{
Console.WriteLine("질럿 이동완료 : ({0},{1})", x, y);
}
private void OnDie()
{
Console.WriteLine("질럿이 죽었습니다.");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class Zealot
{
public Action onDie;
public Action<float, float> onMoveComplete;
private float x;
private float y;
public Zealot(float x, float y)
{
this.x = x;
this.y = y;
}
public void Move(float x, float y)
{
this.x = x;
this.y = y;
this.onMoveComplete(this.x, this.y);
}
public void Die()
{
this.onDie();
}
}
}
================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
public class App
{
//생성자
public App()
{
BookDB bookDB = new BookDB();
bookDB.AddBook("해리포터", 10050, true);
bookDB.AddBook("니클의 소년들", 14000, false);
bookDB.AddBook("멋진 신세계", 8200, true);
//bookDB.ProcessPaperbackBooks(PrintTitle);
bookDB.ProcessBooks(true, PrintTitle);
PriceTotaller totaller = new PriceTotaller();
bookDB.ProcessBooks(true, totaller.AddBookToTotal);
Console.WriteLine("{0}, {1}", totaller.CountBooks, totaller.PriceBooks);
bookDB.FindBookByName("해리포터", FoundBook);
}
private void FoundBook(string title)
{
if (string.IsNullOrEmpty(title))
{
Console.WriteLine("책을 찾을수 없습니다.");
}
else
{
Console.WriteLine("{0}을 찾았습니다.", title);
}
}
private void PrintTitle(Book book) {
Console.WriteLine("{0}, {1}, {2}", book.title, book.price, book.paperBack);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
public struct Book
{
public string title;
public int price;
public bool paperBack;
//생성자
public Book(string title, int price, bool paperBack)
{
this.title = title;
this.price = price;
this.paperBack = paperBack;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
public class PriceTotaller
{
public int CountBooks
{
get; private set;
}
public int PriceBooks
{
get; private set;
}
public void AddBookToTotal(Book book)
{
this.CountBooks += 1;
this.PriceBooks += book.price;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
//델리게이트 정의 (형식정의)
public delegate void ProcessBookCallback(Book book);
public class BookDB
{
private List<Book> list;
//생성자
public BookDB()
{
this.list = new List<Book>();
}
//책추가
public void AddBook(string title, int price, bool paperBack)
{
Book book = new Book(title, price, paperBack);
this.list.Add(book);
}
//종이책을 찾아내서 콜백으로 Book 인스턴스를 전달
public void ProcessPaperbackBooks(ProcessBookCallback callback)
{
foreach (Book book in list)
{
if (book.paperBack)
{
callback(book);
}
}
}
public void ProcessBooks(bool paperBack, ProcessBookCallback callback)
{
foreach (Book book in list)
{
if (book.paperBack == paperBack)
{
callback(book);
}
}
}
public void FindBookByName(string title, Action<string> callback)
{
foreach (Book book in list)
{
if (book.title == title)
{
callback(book.title);
return;
}
}
callback(string.Empty);
}
}
}
================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
public class App
{
//생성자
public App()
{
Oven oven = new Oven();
oven.onBake = this.OnBake;
oven.Bake(new Pizza());
}
private void OnBake()
{
Console.WriteLine("피자가 다 구워졌습니다.");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class Pizza
{
public Pizza()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Homework1234
{
public delegate void OnBake();
class Oven
{
public OnBake onBake;
public Oven()
{
}
public void Bake(Pizza pizza)
{
Console.WriteLine("피자 굽기 시작");
Thread.Sleep(3000);
this.onBake();
}
}
}
================================================
App클래스의 Build메서드에 자꾸 에러가 표시된다. 이유를 모르겠다
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Homework1234
{
public class App
{
public App()
{
Building b = new Building();
b.onBuildComplete = this.OnBuildComplete;
this.Build(b);
}
private void OnBuildComplete()
{
Console.WriteLine("건설완료");
}
public void Build(Building b)
{
b.StartBuildProcess();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Homework1234
{
public delegate void DelOnBuildComplete();
class Building
{
public DelOnBuildComplete onBuildComplete;
public Building()
{
}
public void StartBuildProcess()
{
Console.WriteLine("건설을 시작합니다.");
for(int i = 0; i <= 10; i++)
{
Thread.Sleep(500);
Console.WriteLine("{0}%", i * 10);
}
this.onBuildComplete();
}
}
}
================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Homework1234
{
public class App
{
private Hero hero;
private Popup popup;
public App()
{
hero = new Hero();
popup = new Popup();
popup.Print();
hero.onAttack = this.OnAttack;
hero.Attack();
hero.Attack();
hero.Attack();
hero.Attack();
hero.Attack();
hero.Attack();
hero.Attack();
hero.Attack();
hero.Attack();
hero.Attack();
}
private void OnAttack()
{
if (popup.IsComplete())
{
this.hero.onAttack = null;
}
this.popup.UpdateInfo();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class Popup
{
public int goal = 8;
public int count = 0;
public Popup()
{
}
public bool IsComplete()
{
return count >= goal;
}
public void Print()
{
if (this.IsComplete()) //IsComplete()가 true인지 false인지? 봐서는 true인것같은데
{
Console.WriteLine("[완료] 놀 처치 {0}/{1}", this.count, this.goal);
}
else
{
Console.WriteLine("놀 처치 {0}/{1}", this.count, this.goal);
}
}
public void UpdateInfo()
{
if(this.count >= this.goal)
{
return;
}
this.count++;
this.Print();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework1234
{
class Hero
{
public Action onAttack;
public Hero()
{
}
public void Attack()
{
Console.WriteLine("놀을 처치했습니다.");
if (this.onAttack != null)
{
this.onAttack();
}
}
}
}
================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Homework1234
{
public class App
{
private Goblin goblin;
public App()
{
this.goblin = new Goblin();
goblin.onAttackComplete = this.OnAttackComplete;
goblin.ChangeState(Goblin.eState.Idle);
goblin.ChangeState(Goblin.eState.Attack);
}
private void OnAttackComplete()
{
this.goblin.ChangeState(Goblin.eState.Idle);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Homework1234
{
class Goblin
{
public Action onAttackComplete;
public enum eState
{
Idle,
Attack
}
public Goblin()
{
}
public void ChangeState(eState state)
{
switch (state)
{
case eState.Idle:
this.Idle();
break;
case eState.Attack:
this.onAttackComplete();
break;
}
}
private void Idle()
{
Console.WriteLine("Idle");
}
private void Attack()
{
for(int i = 0; i < 5; i++)
{
Console.WriteLine("goblin_attack_0{0}", i);
Thread.Sleep(500);
}
this.onAttackComplete();
}
}
}
'C# > 수업내용' 카테고리의 다른 글
C# 21.03.24.수업내용 (0) | 2021.03.24 |
---|---|
C# 21.03.23.수업내용 (0) | 2021.03.23 |
C# 21.03.19.수업내용 (0) | 2021.03.19 |
C# 21.03.18.수업내용 (0) | 2021.03.18 |
C# 21.03.17.수업내용 (0) | 2021.03.17 |