유니티3D 프로그래밍
C# 3주차 1일 수업 과제 : Delegate, Action, Callback (21.03.22) 본문
로켓 발사
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
public class App
{
//생성자
public App()
{
Rocket rocket = new Rocket();
rocket.onRocket = this.RocketLauncher;
rocket.RocketIngition();
}
public void RocketLauncher()
{
Console.WriteLine("로켓이 발사되었습니다.");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
public delegate void RocketLauncher();
public class Rocket
{
public RocketLauncher onRocket;
//생성자
public Rocket()
{
}
public void RocketIngition()
{
for(int i = 10; i > 0; i--)
{
Console.WriteLine("로켓 발사 준비. {0}", i);
}
this.onRocket();
}
}
}
Valture Mine
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
public class App
{
private Valture valture;
private Mine mine;
//생성자
public App()
{
valture = new Valture();
mine = new Mine();
valture.onMine = this.Install;
valture.Move(10, 8);
valture.InstallMine();
}
public void Install()
{
Console.WriteLine("마인이 설치되었습니다.");
this.mine.GroundedMinePosition(valture.x, valture.y);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
public delegate void OnMine();
public class Valture
{
public OnMine onMine;
public float x;
public float y;
public int Hp { get; private set; }
public int MaxHP { get; private set; }
public Valture()
{
this.MaxHP = 70;
this.Hp = this.MaxHP;
}
public void InstallMine()
{
Console.WriteLine("마인을 설치합니다.");
this.onMine();
}
public void Move(float x, float y)
{
this.x = x;
this.y = y;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
public class Mine
{
private float x;
private float y;
public Mine()
{
}
public void GroundedMinePosition(float x, float y)
{
this.x = x;
this.y = y;
Console.WriteLine("[{0}], [{1}] 위치에 마인이 설치되었습니다.", this.x, this.y);
}
}
}
과일 저장
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
public class App
{
public Store store;
public Fruit fruits;
//생성자
public App()
{
fruits = new Fruit("사과");
store = new Store();
fruits.storeFruits = this.OnStore;
fruits.StoreFruits();
Console.WriteLine();
fruits = new Fruit("배");
fruits.storeFruits = this.OnStore;
fruits.StoreFruits();
Console.WriteLine();
store.ListStore();
}
public void OnStore(string name)
{
Console.WriteLine("{0}가 저장되었습니다.", name);
store.StoreFruits(this.fruits);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
public delegate void DelFruitsStore(string name);
public class Fruit
{
public DelFruitsStore storeFruits;
public string Name { get; private set; }
//생성자
public Fruit(string name)
{
this.Name = name;
}
public void StoreFruits ()
{
Console.WriteLine("{0}을/를 보관합니다.", this.Name);
this.storeFruits(this.Name);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
public class Store
{
List<Fruit> fruits = new List<Fruit>();
public Store()
{
}
public void StoreFruits(Fruit fruits)
{
this.fruits.Add(fruits);
}
public void ListStore()
{
Console.WriteLine("저장된 과일을 보여줍니다.");
foreach (Fruit fruit in fruits)
{
Console.WriteLine("{0}",fruit.Name);
}
}
}
}
과일 저장 Action
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
public class Fruit
{
public Action<string> storeFruits;
public string Name { get; private set; }
//생성자
public Fruit(string name)
{
this.Name = name;
}
public void StoreFruits ()
{
Console.WriteLine("{0}을/를 보관합니다.", this.Name);
this.storeFruits(this.Name);
}
}
}
간단한 callback
using System;
using System.Collections;
using System.Collections.Generic;
namespace Study01
{
public class App
{
//생성자
public App()
{
Gun gun = new Gun();
gun.fire = this.GunShot;
gun.Shot(GunShot);
}
public void GunShot()
{
Console.WriteLine("총알이 발사되었습니다.");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study01
{
public delegate void Shoot();
public class Gun
{
public Shoot fire;
public Gun()
{
}
public void Shot(Shoot callback)
{
Console.WriteLine("총을 쏩니다.");
callback();
}
}
}
'C# > 수업과제' 카테고리의 다른 글
C# 게임 알고리즘 시험 (0) | 2021.03.30 |
---|---|
C# 3주차 3일 수업 과제 : File (21.03.24) (0) | 2021.03.24 |
C# 2주차 5일 수업 내용 및 과제 : 2차원 배열 및 2048 프로그램(21.03.19) (0) | 2021.03.19 |
C# 2주차 3일 수업 과제 : Stack (21.03.17) (0) | 2021.03.17 |
C# 2주차 2일 수업 과제 (21.03.16) (0) | 2021.03.16 |