아카이브
Action 대리자 연습 본문
1.
히어로 클래스
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
public class Hero
{
public Hero()
{
Console.WriteLine("영웅이 생성되었습니다.");
}
public void Move(Action callback)
{
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동완료");
callback();
}
}
}
앱
using LearnDotnet;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
//using 지시문을 사용하면 네임스페이스에 정의된 형식을 해당 형식의 정규화된 네임스페이스를 지정하지 않고도 사용할 수 있습니다.
namespace LearnDotnet
{
public class App
{
//생성자
public App()
{
//대리자 초기화 (대리자 인스턴스화/메서드 연결
Action moveComplete = () =>
{
Console.WriteLine("영웅 이동완료");
};
Hero hero = new Hero();
hero.Move(moveComplete); //대리자 인스턴스를 인수로 전달
}
}
}
2.
히어로 클래스
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
public class Hero
{
//맴버
public Action moveComplete; //대리자 변수 정의
//생성자
public Hero()
{
Console.WriteLine("영웅이 생성되었습니다.");
}
public void Move()
{
Console.WriteLine("이동중..");
Console.WriteLine("이동중..");
Console.WriteLine("이동완료");
this.moveComplete();
}
}
}
앱
using LearnDotnet;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
//using 지시문을 사용하면 네임스페이스에 정의된 형식을 해당 형식의 정규화된 네임스페이스를 지정하지 않고도 사용할 수 있습니다.
namespace LearnDotnet
{
public class App
{
//생성자
public App()
{
Hero hero = new Hero();
//대리자 초기화
hero.moveComplete = () =>
{
Console.WriteLine("영웅이 이동완료했습니다");
};
hero.Move();
}
}
}
3. 공격추가
히어로 클래스
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
public class Hero
{
//맴버
public Action onAttackComplete; //대리자 변수 정의
//생성자
public Hero()
{
}
public void Attack()
{
//공격 애니메이션
Console.WriteLine("공격중...");
Console.WriteLine("공격이 빗나갔습니다.");
Console.WriteLine("공격이 적중했습니다.");
Console.WriteLine("공격완료");
//공격 완료
this.onAttackComplete();
}
//이동
public void Move(Action callback)
{
Console.WriteLine("이동중...");
Console.WriteLine("이동중...");
Console.WriteLine("이동완료");
callback(); //대리자에 연결된 메서드가 호출된다
}
}
}
앱
using LearnDotnet;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
//using 지시문을 사용하면 네임스페이스에 정의된 형식을 해당 형식의 정규화된 네임스페이스를 지정하지 않고도 사용할 수 있습니다.
namespace LearnDotnet
{
public class App
{
//생성자
public App()
{
Hero hero = new Hero();
//대리자 초기화
hero.onAttackComplete = () =>
{
Console.WriteLine("영웅이 공격을 완료했습니다.\n");
};
hero.Attack();
hero.Move(() =>
{
Console.WriteLine("영웅이 이동했습니다.");
});
}
}
}
'C#프로그래밍' 카테고리의 다른 글
json 연습(몬스터, 영웅, 아이템, 인벤토리 ) (0) | 2023.07.28 |
---|---|
Action 대리자 연습(HitDamage) (0) | 2023.07.27 |
Dictionary 인벤토리* (0) | 2023.07.26 |
Dictionary 연습2 (0) | 2023.07.26 |
Dictionary 연습 (0) | 2023.07.26 |