대리자2, Action, Func, 람다활용,
Action&Func
-Action과 func를 사용해서 대리자를 편하게 선언(미리 선언된 대리자들)
Action
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
public class App
{
//생성자
public App()
{
//대리자 변수 정의
Action action;
//대리자 인스턴스화(메서드 연결)
action = this.SayHello;
//람다문
action = () =>
{
Console.WriteLine("Hello~");
};
action();
}
void SayHello()
{
}
}
}
Func는 값을 반환할 때 사용한다.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
public class App
{
//생성자
public App()
{
//대리자 변수 정의
Func<int> func;
//대리자 인스턴스화(메서드 연결)
func = this.GetHp;
//대리자 사용
int result = func();
Console.WriteLine(result);
}
int GetHp()
{
return 10;
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
public class App
{
//생성자
public App()
{
//대리자 변수 정의
Func<int, int, int> func;
//대리자 인스턴스화(메서드 연결)
func = (a, b) =>
{
return a + b;
};
//대리자 사용
int result = func(1,2);
Console.WriteLine(result);
}
}
}
람다활용 1. callback 사용, Action을 App에 사용
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
public class App
{
//생성자
public App()
{
Action moveComplete = () => {
Console.WriteLine("이동 완료 했습니다!");
};//대리자 초기화(대리자 인스턴스화, 메서드 연결)
Hero hero = new Hero();
hero.Move(moveComplete);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class Hero
{
//생성자
public Hero() {
Console.WriteLine("영웅이 생성되었습니다.");
}
//메서드
public void Move(Action callback)
{
Console.WriteLine("이동중.....");
Console.WriteLine("이동중.....");
Console.WriteLine("이동중.....");
Console.WriteLine("이동중.....");
Console.WriteLine("이동중.....");
callback();
}
}
}
람다활용 2. Action 을 Hero에 정의, callback은 사용안하고 moveComplete는 App에 작성
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
public class App
{
//생성자
public App()
{
Hero hero = new Hero();
hero.moveComplete = () =>
{
Console.WriteLine("이동 완료했습니다~");
};
hero.Move();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class Hero
{
//멤버
public Action moveComplete;//대리자 변수 정의
//생성자
public Hero() {
Console.WriteLine("영웅이 생성되었습니다.");
}
//메서드
public void Move()
{
Console.WriteLine("이동중.....");
Console.WriteLine("이동중.....");
Console.WriteLine("이동중.....");
Console.WriteLine("이동중.....");
Console.WriteLine("이동중.....");
this.moveComplete();
}
}
}
람다활용 3.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
public class App
{
//생성자
public App()
{
Hero hero = new Hero();
hero.Move(() => {
Console.WriteLine("영웅 이동~");
}) ;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class Hero
{
//멤버
public Action moveComplete;//대리자 변수 정의
//생성자
public Hero() {
Console.WriteLine("영웅이 생성되었습니다.");
}
//메서드
public void Move(Action callback)
{
Console.WriteLine("이동중.....");
Console.WriteLine("이동중.....");
Console.WriteLine("이동중.....");
Console.WriteLine("이동중.....");
Console.WriteLine("이동중.....");
callback();
}
}
}
데이터 관리하는 예제
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
public class App
{
//생성자
public App()
{
DataManager.instance.loadComplete = () =>
{
Console.WriteLine("데이터 로드 완료!");
};
DataManager.instance.LoadDatas();
DataManager.instance.loadComplete();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class DataManager
{
public static readonly DataManager instance = new DataManager();
//대리자 변수 정의
public Action loadComplete;
private DataManager() {
}
public void LoadDatas()
{
Console.WriteLine("데이터 로드중....");
Console.WriteLine("데이터 로드중....");
Console.WriteLine("데이터 로드중....");
}
}
}
https://angliss.cc/c-sharp-action/
매개변수가 있는 Action
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
public class App
{
//생성자
public App()
{
Hero hero = new Hero();
hero.HitDamage(3, (hp, maxHp) =>
{
Console.WriteLine("{0}/{1}", hp, maxHp);
});
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class Hero
{
//멤버
public Action moveComplete;//대리자 변수 정의
//생성자
public Hero() {
Console.WriteLine("영웅이 생성되었습니다.");
}
//메서드
public void Move(Action callback)
{
Console.WriteLine("이동중.....");
Console.WriteLine("이동중.....");
Console.WriteLine("이동중.....");
Console.WriteLine("이동중.....");
Console.WriteLine("이동중.....");
callback();
}
public void HitDamage(int hp, Action<int, int> callback)
{
callback(1, 3);
}
}
}
위의 Hero 클래스에 다음 메서드 추가.
App클래스 수정후 출력
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
public class App
{
//생성자
public App()
{
Hero hero = new Hero();
hero.HitDamage(3, (isDie) =>
{
Console.WriteLine("isDie: {0}", isDie);
});
}
}
}
'C# 기초' 카테고리의 다른 글
JSON, 데이터 관리 연습 (0) | 2023.07.28 |
---|---|
JSON (0) | 2023.07.27 |
대리자 (0) | 2023.07.27 |
개체 이니셜라이저 (0) | 2023.07.26 |
가짜 인벤토리 만들기 (0) | 2023.07.26 |