형식이 같은 두 개체를 비교할때 자주 사용하는 Comparsion 델리게이트이다.
public class m_test : MonoBehaviour
{
public delegate int Comparsion<in T>(T x, T y);
static int SortWithPrice(Product a, Product b)
{
return a.price.CompareTo(b.price);
}
class Product
{
public string Name { get; set; }
public int price { get; set; }
}
void Start()
{
List<Product> products = new List<Product>()
{
new Product() { Name = "감자", price = 500 },
new Product() { Name = "사과", price = 700 },
new Product() { Name = "고구마", price = 400 },
new Product() { Name = "배추", price = 600 },
new Product() { Name = "상추", price = 300 }
};
products.Sort(SortWithPrice);
foreach(var val in products)
{
Debug.Log(string.Format("품목:{0}, 가격:{1}",val.Name, val.price));
}
}
}
public delegate int Comparison<in T>(T x, T y);
T : 비교할 개체의 형식,
x : 비교할 첫 번째 개체
y : 비교할 두 번째 개체
public class m_test : MonoBehaviour
{
//public delegate int Comparsion<in T>(T x, T y);
//static int SortWithPrice(Product a, Product b)
//{
// return a.price.CompareTo(b.price);
//}
class Product
{
public string Name { get; set; }
public int price { get; set; }
}
void Start()
{
List<Product> products = new List<Product>()
{
new Product() { Name = "감자", price = 500 },
new Product() { Name = "사과", price = 700 },
new Product() { Name = "고구마", price = 400 },
new Product() { Name = "배추", price = 600 },
new Product() { Name = "상추", price = 300 }
};
products.Sort(delegate (Product a, Product b)
{
return a.price.CompareTo(b.price);
});
//products.Sort(SortWithPrice);
foreach(var val in products)
{
Debug.Log(string.Format("품목:{0}, 가격:{1}",val.Name, val.price));
}
}
}
=> SortWithPrice 메서드를 선언하지 않고도 가능하다.
products.Sort((a, b) =>
{
return a.price.CompareTo(b.price);
});
=> 람다 사용
콜백메서드 구현하기
class Monster
{
public string Name { get; set; }
public double Pow { get; set; }
public Monster(string name, double pow)
{
Name = name;
Pow = pow;
}
public override string ToString()
{
return this.Name+" : " +this.Pow;
}
}
Monster.class 몬스터이름과 파워수치를 받는 클래스
class Monsters
{
private List<Monster> listOfMonster = new List<Monster>();
public delegate void PrintProcess(Monster list);
public void Add(Monster monster)
{
listOfMonster.Add(monster);
}
public void Print()
{
Print((monseter) =>
{
Console.WriteLine(monseter);
});
}
public void Print(PrintProcess porocess)
{
//콜백메서드를 사용
foreach(var item in listOfMonster)
{
porocess(item); //콜백 메서드에 매개변수를 전달해 호출
}
}
}
Mosters.class
Add메서드를 통해 Monster클래스를 가지고있는 객체들을 리스트로 넣어주고,
델리게이트를 통해 몬스터 리스트들의 정보를 출력해준다.
monsters.Print(); : monster.class ToString을 통해서 출력
monsters.Print((monster) =>
{
Console.WriteLine();
Console.WriteLine("이름: " + monster.Name);
Console.WriteLine("파워: " + monster.Pow);
});
: 델리게이트를 통해 출력형식을 지정한 출력
풀 코드
...더보기
using System;
using System.Collections.Generic;
namespace ConsoleApp23
{
class Program
{
class Monster
{
public string Name { get; set; }
public double Pow { get; set; }
public Monster(string name, double pow)
{
Name = name;
Pow = pow;
}
public override string ToString()
{
return this.Name + " : " + this.Pow;
}
}
class Monsters
{
private List<Monster> listOfMonster = new List<Monster>();
public delegate void PrintProcess(Monster list);
public void Add(Monster monster)
{
listOfMonster.Add(monster);
}
public void Print()
{
Print((monseter) =>
{
Console.WriteLine(monseter);
});
}
public void Print(PrintProcess porocess)
{
foreach (var item in listOfMonster)
{
porocess(item);
}
}
}
static void Main(string[] args)
{
Monsters monsters = new Monsters();
monsters.Add(new Monster("고블린", 100));
monsters.Add(new Monster("슬라임", 10));
monsters.Print();
monsters.Print((monster) =>
{
Console.WriteLine();
Console.WriteLine("이름: " + monster.Name);
Console.WriteLine("파워: " + monster.Pow);
});
}
}
}
'삽질&복습(연습장)' 카테고리의 다른 글
유니티) 백그라운드 시간 흐름 테스트 (Unscaled Time, FixedDeltaTime, RealTime) (0) | 2020.07.30 |
---|---|
ExtensionMethod 활용하기_테이블 데이터 (0) | 2019.05.30 |
유니티_복습) 오브젝트 풀링하기 (1) | 2019.05.08 |
복습)C#,유니티 제네릭 (0) | 2019.05.04 |
삽질) 물리연산의 체크사항, 레이캐스트 (0) | 2019.05.02 |