using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//1. 대리자 형식 정의
delegate void MyDel();
//생성자
public App()
{
//2. 대리자 인스턴스화 (람다/무명메서드 연결)
MyDel del = () => {
Console.WriteLine("안녕하세요");
};
//3. 대리자 사용
del();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//1. 대리자 형식 정의
delegate void MyDel(string name);
//생성자
public App()
{
//2. 대리자 인스턴스화 (람다/무명메서드 연결)
MyDel del = (name) => {
Console.WriteLine(name);
};
//3. 대리자 사용
del("홍길동");
}
}
}
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//1. 대리자 형식 정의
delegate int MyDel(int a, int b);
//생성자
public App()
{
//2. 대리자 인스턴스화 (람다/ 무명메서드연결)
MyDel del = (a, b) => {
return a + b;
};
//3. 대리자 호출
Console.WriteLine(del(1, 2));
}
}
}
App
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//1. 대리자 형식 정의
public delegate void DelHeroMoveComplete();
//생성자
public App()
{
//2. 대리자 인스턴스화 (람다/메서드연결)
DelHeroMoveComplete del = () => {
Console.WriteLine("영웅이 이동을 완료했습니다.");
};
Hero hero = new Hero();
hero.Move(del);
}
}
}
Hero
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
internal class Hero
{
//생성자
public Hero()
{
Console.WriteLine("영웅이 생성되었습니다.");
}
public void Move(App.DelHeroMoveComplete callback)
{
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동완료");
callback();
}
}
}
Action : 값을 반환하지 않는 경우
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//생성자
public App()
{
//대리자 변수 정의
Action action;
//대리자 인스턴스화 (메서드연결)
action = this.SayHello;
action = () => {
Console.WriteLine("Hello ~");
}; //람다문
action(); // 아래꺼가 실행됌
}
void SayHello()
{
}
}
}
Func : 값을 반환하는 경우
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//생성자
public App()
{
//Func 대리자 변수 정의
Func<int> func;
//대리자 인스턴스화 (메서드연결)
func = this.GetHp;
//대리자 사용
int hp = func();
Console.WriteLine(hp);
}
int GetHp()
{
return 10;
}
}
}
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//생성자
public App()
{
//Func대리자 변수 정의
Func<int, int, int> func;
func = (a, b) => {
return a + b;
};
func = Sum;
int sum = func(1, 2);
Console.WriteLine(sum);
}
int Sum(int a, int b)
{
return a + b;
}
}
}
App
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//생성자
public App()
{
//대리자 초기화[대리자 인스턴스화 / 메서드 연결]
Action moveComplete = () => {
Console.WriteLine("영웅 이동완료");
};
Hero hero = new Hero();
hero.Move(moveComplete); //대리자 인스턴스를 인수로 전달
}
}
}
Hero
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
internal class Hero
{
//생성자
public Hero()
{
Console.WriteLine("영웅이 생성되었습니다.");
}
public void Move(Action callback)
{
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동완료");
callback();
}
}
}
App
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//생성자
public App()
{
Hero hero = new Hero();
//대리자 인스턴스화
hero.moveComplete = () => {
Console.WriteLine("영웅이 이동완료 했습니다.");
};
hero.Move(); //대리자 인스턴스를 인수로 전달
}
}
}
Hero
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
internal class Hero
{
//멤버
public Action moveComplete; // 대리자 변수 정의
//생성자
public Hero()
{
Console.WriteLine("영웅이 생성되었습니다.");
}
public void Move()
{
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동완료");
this.moveComplete();
}
}
}
App
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//생성자
public App()
{
Hero hero = new Hero();
hero.Move(() => {
Console.WriteLine("영웅이 이동완료했습니다.");
}); //대리자 인스턴스를 인수로 전달
}
}
}
Hero
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
internal class Hero
{
//생성자
public Hero()
{
Console.WriteLine("영웅이 생성되었습니다.");
}
public void Move(Action callback)
{
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동완료");
callback();
}
}
}
App 클래스 [싱글톤 사용]
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//생성자
public App()
{
DataManager.instance.loadComplete = () => {
Console.WriteLine("데이터 로드 완료");
};
DataManager.instance.LoadDatas();
}
}
}
DataManager 클래스 [싱글톤]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class DataManager
{
public static DataManager instance = new DataManager();
public Action loadComplete;
//생성자
public DataManager()
{
}
//데이터 로드
public void LoadDatas()
{
Console.WriteLine("데이터 로드중...");
Console.WriteLine("데이터 로드중...");
Console.WriteLine("데이터 로드중...");
Console.WriteLine("데이터 로드중...");
Console.WriteLine("데이터 로드중...");
this.loadComplete();
}
}
}
Hero
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class Hero
{
//생성자
public Hero()
{
}
//이동
public void Move(Action callback) // callback = 대리자 인스턴스 : 익명 메서드와 연결된 값
{
Console.WriteLine("이동중...");
callback(); //대리자에 연결된 메서드가 호출된다
}
}
}
App
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//생성자
public App()
{
Hero hero = new Hero();
hero.Move(() => {
Console.WriteLine("영웅 이동완료");
});
//DataManager.instance.loadComplete = () => {
// Console.WriteLine("데이터 로드 완료");
//};
//DataManager.instance.LoadDatas();
}
}
}
Action 연습
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//생성자
public App()
{
Hero hero = new Hero();
hero.Attack(() => {
Console.WriteLine("영웅 공격완료");
});
hero.moveComplete = () => {
Console.WriteLine("영웅 이동완료");
};
hero.Move();
//DataManager.instance.loadComplete = () => {
// Console.WriteLine("데이터 로드 완료");
//};
//DataManager.instance.LoadDatas();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class Hero
{
public Action moveComplete;
//생성자
public Hero()
{
}
//이동
public void Move()
{
Console.WriteLine("이동중...");
this.moveComplete();
}
//공격
public void Attack(Action callback)
{
Console.WriteLine("공격중...");
callback();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class Hero
{
public Action moveComplete;
private int hp;
private int maxHp;
//생성자
public Hero(int maxHp)
{
this.maxHp = maxHp;
this.hp = this.maxHp;
}
//이동
public void Move()
{
Console.WriteLine("이동중...");
this.moveComplete();
}
//공격
public void Attack(Action callback)
{
Console.WriteLine("공격중...");
callback();
}
public void HitDamage(int damage, Action<int, int> callback)
{
Console.WriteLine("(-{0})데미지를 받았습니다.", damage);
this.hp -= damage;
callback(this.hp, this.maxHp);
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//생성자
public App()
{
Hero hero = new Hero(30);
hero.HitDamage(3, (hp, maxHp) => {
Console.WriteLine("{0}/{1}", hp, maxHp);
});
//DataManager.instance.loadComplete = () => {
// Console.WriteLine("데이터 로드 완료");
//};
//DataManager.instance.LoadDatas();
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//생성자
public App()
{
Hero hero = new Hero(3);
hero.HitDamage(3, (isDie) => {
Console.WriteLine("isDie: {0}", isDie);
});
//DataManager.instance.loadComplete = () => {
// Console.WriteLine("데이터 로드 완료");
//};
//DataManager.instance.LoadDatas();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class Hero
{
public Action moveComplete;
private int hp;
private int maxHp;
//생성자
public Hero(int maxHp)
{
this.maxHp = maxHp;
this.hp = this.maxHp;
}
//이동
public void Move()
{
Console.WriteLine("이동중...");
this.moveComplete();
}
//공격
public void Attack(Action callback)
{
Console.WriteLine("공격중...");
callback();
}
public void HitDamage(int damage, Action<bool> callback)
{
Console.WriteLine("(-{0})데미지를 받았습니다.", damage);
this.hp -= damage;
Console.WriteLine("{0}/{1}", this.hp, this.maxHp);
if (hp <= 0)
callback(true);
else
callback(false);
}
}
}
이것도 가능
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class Hero
{
public Action moveComplete;
private int hp;
private int maxHp;
//생성자
public Hero(int maxHp)
{
this.maxHp = maxHp;
this.hp = this.maxHp;
}
//이동
public void Move()
{
Console.WriteLine("이동중...");
this.moveComplete();
}
//공격
public void Attack(Action callback)
{
Console.WriteLine("공격중...");
callback();
}
public void HitDamage(int damage, Action<bool> callback)
{
this.hp -= damage;
Console.WriteLine("(-{0})데미지를 받았습니다.({1}/{2})", damage, this.hp, this.maxHp);
callback(this.hp <= 0);
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class App
{
//생성자
public App()
{
ItemFactory factory = new ItemFactory();
factory.CreateItem(100, (item) => {
Console.WriteLine("{0}이 생성되었습니다.", item.name);
});
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class Item
{
public int id;
public string name;
//생성자
public Item(int id, string name)
{
this.id = id;
this.name = name;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FakeInventory
{
public class ItemFactory
{
//생성자
public ItemFactory()
{
}
public void CreateItem(int id, Action<Item> callback)
{
Item item = new Item(id, "홍길동");
callback(item);
}
}
}
void SayHello(Action callback) {
callback();
}
이거는
SayHello(new Action(this.Method));에서 new Action이 생략된거임
void Method() {
}
'C#프로그래밍' 카테고리의 다른 글
JSON (0) | 2023.07.27 |
---|---|
엑셀데이터를 JSON, JSON online viewer (0) | 2023.07.27 |
대리자, 무명메서드 (0) | 2023.07.27 |
List, Dictionary 복습 (0) | 2023.07.27 |
과제 (0) | 2023.07.26 |