1.callback
(1)App.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class App
{
//생성자
public App()
{
Hero hero = new Hero();
Action action = () => {
Console.WriteLine("영웅이 이동을 완료했습니다.");
};
hero.Move(action);
}
}
}
or
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class App
{
//생성자
public App()
{
Hero hero = new Hero();
hero.Move(() => {
Console.WriteLine("영웅이 이동을 완료했습니다.");
});
}
}
}
(2)Hero.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class Hero
{
public Hero()
{
}
public void Move(Action callback)
{
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동완료");
callback();
}
}
}
2.멤버 변수로 사용
(1) App.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class App
{
//생성자
public App()
{
Hero hero = new Hero();
hero.moveCompleteAction = () =>
{
Console.WriteLine("영웅이 이동을 완료했습니다.");
};
hero.Move();
}
}
}
(2)Hero.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class Hero
{
public Action moveCompleteAction;
public Hero()
{
}
public void Move()
{
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동완료");
moveCompleteAction();
}
}
}
결과
'K-digital traning > C#프로그래밍' 카테고리의 다른 글
대리자 연습 - Hero HitDamage (0) | 2023.07.27 |
---|---|
대리자 연습 - 데이터매니저 로드 (0) | 2023.07.27 |
가짜 인벤토리 만들기 (0) | 2023.07.26 |
2차원 배열 연습 (0) | 2023.07.25 |
1차원 배열 인벤토리 복습 (+ 인벤토리 용량확장 (0) | 2023.07.25 |