0324 복습
2021. 3. 24. 16:53ㆍC#/수업내용
public delegate void DelPrint(string message);
public class Printer
{
public Printer() { }
public void Print(string msg)
{
Console.WriteLine(msg);
}
}
public class App
{
public App()
{
string message = "동해물과 백두산이...";
//객체 생성
Printer printer = new Printer();
//대리자 인스턴스 화
DelPrint delPrinter = new DelPrint(printer.Print);
delPrinter(message);
DelPrint delPrinter2 = delegate (string msg)
{
Console.WriteLine("=======무명메서드=======");
Console.WriteLine(msg);
};
//대리자 호출
delPrinter2(message);
}
}
//대리자 선언
public delegate void DelPrint(string message);
public delegate string DelPrintWithLogo(string logo, string message);
public class Printer
{
public Printer() { }
public void Print(string msg)
{
Console.WriteLine(msg);
}
public string PrintWithLogo(string logo, string message)
{
return logo + message;
}
}
public class App
{
public App()
{
string message = "동해물과 백두산이...";
//객체 생성
Printer printer = new Printer();
//대리자 인스턴스 화
DelPrint delPrinter = new DelPrint(printer.Print);
delPrinter(message);
DelPrint delPrinter2 = delegate (string msg)
{
Console.WriteLine("=======무명메서드=======");
Console.WriteLine(msg);
};
//대리자 호출
delPrinter2(message);
//람다식
DelPrint delPrinter3 = (msg) =>
{
Console.WriteLine("=======람다식=======");
Console.WriteLine(msg);
};
//대리자 호출
delPrinter3(message);
string logo = @"``` `` `` ``` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` ``
`` `` ``` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` `` ```
````````````````````````````````````````````````````````````````````````````````````````````````````
``` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` ```+:` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` ``
`` `` ``` `` `` ``` `` `` ``` `` `` ``` `` `` ``` oMN`` ``` `` `` ``` `` `` ``` `` `` ``` `` `` ```
``` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` ``yMd` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` ``
`````````````````````````````````````````````````sMN-```````````````````````````````````````````````
`` `` ``` `` `` `````` `` ```-/ossoo/-``````````hMm-``` ``````-/ossoo+:````````````````````````````
``` `` `` ``` `` `` ``` `/hMMmhyyydNMmo....`..:MM:...`..`..omMNhyyyhmMMd+``` `` ``` `` `` ``` `` ``
`` `` ``` `` `` ``` `` `/mMd/``` `` `:hMMMMMMMMMMMMMMMMMMMMMMh-`` ``` ./hMN/``` `` `` ``` `` `` ```
``` `` `` ``` `` `` ``/MM/ `` ``` `` `.`...`..`..`...`..`..```` `` `` ``:NMo `` ``` `` `` ``` `` ``
```````````````````````NMo``````.```````````````````````````shh+```ohho```+MM.``````````````````````
`` `` ``` `` `` ``` ``-MM.``` `NM+` ``` `` `` ``` `` `` ```+MMMM- /MMMM: `.MM:` `` `` ``` `` `` ```
``` `` `` ``` `` `` `+MM``.+o+MMy+o: `` ```:o++:` -o++:` ``/ss: ``:ss:``` MM+`` `` `` `` `` `` ``
`` `` ``` `` `` ``` ``sMm `:hdhMMmhds`` `` `sddho `oddhs``` .:-`` `.--.` ``mMs` `` `` ``` `` `` ```
``````````````````````hMh````` MM+`````````````````````````/MMMm.`:NMMN-```hMh``````````````````````
``````````````````````mMs``````/o.`````````````````````````:mMMh``-dMMd.```sMm``````````````````````
`` `` ``` `` `` ``` ``MM+ ``` `` `` ``` `` `` sNNNNNNs` ``` `.``` `````` ``+MM` `` `` ``` `` `` ```
``` `` `` ``` `` `` .MM-`` `` ``` .ydo. ```-yMN/::/NMh/` ```/yo `` `` ``` :MM-` ``` `` `` ``` `` ``
`` `` ``` `` `` ``` `/MM. ``` `` ``yMNNMmhdNMNs.` ```odMMNmNMMNMs ``` `` ``.MM: `` `` ``` `` `` ```
`````````````````````+MM``````````sMN-`:+o+/-```````````-:::-`-NMo``````````MM+`````````````````````
``` `` `` ``` `` `` -MM/`` `` ``+MM: `` ``` `` `` ``` `` `` ``:MM+ `` ``` :MM:` ``` `` `` ``` `` ``
`` `` ``` `` `` ``` ``+MMs.`` ``+MM/``` `` `` ``` `` `` ``` `` `/MM+` `` .oNMs` `` `` ``` `` `` ```
``` `` `` ``` `` `` ``-yNMmddmMMh:`` `` ``` `` `` ``` `` `` ``` :hMMmddmMMh: `` ``` `` `` ``` `` ``
``````````````````````````-/++/:````````````````````````````````````:/++/:``````````````````````````
````````````````````````````````````````````````````````````````````````````````````````````````````
``` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` ``
`` `` ``` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` `` ``` `` `` ```
";
//대리자 인스턴스 화
DelPrintWithLogo printer4 = new DelPrintWithLogo(printer.PrintWithLogo);
//대리자 호출
string paper=printer4(logo, "게임 시작");
Console.WriteLine(paper);
//람다식
DelPrintWithLogo printer5 = (logo1, message1) => string.Format("", logo, message);
string paper2 = printer5(logo, "게임 시작");
Console.WriteLine("=========람다식=========");
Console.WriteLine(paper2);
}
}
메서드의 콜백
//RCCar
public class RCCar
{
public enum eDirection {
FORWARD, BACKWORD, LEFT, RIGHT
}
public void Move(eDirection dir) {
Console.WriteLine("{0}로 이동합니다.", dir);
}
}
public class RemoteController
{
//대리자 선언
public delegate void DelMove(RCCar.eDirection dir);
private RCCar car;
private DelMove delMove;
//생성자
public RemoteController() {
}
public void Connect(RCCar car) {
this.car = car;
this.delMove = new DelMove(this.car.Move);
Console.WriteLine("{0}와 연결되었습니다.", this.car);
}
public void Control(RCCar.eDirection dir) {
this.delMove(dir);
}
}
public class App
{
public App()
{
Console.WriteLine("App");
//메서드의 매개변수에 대리자형식 사용하기
RCCar car = new RCCar();
RemoteController controller = new RemoteController();
controller.Connect(car);
controller.Control(RCCar.eDirection.FORWARD);
controller.Control(RCCar.eDirection.FORWARD);
controller.Control(RCCar.eDirection.BACKWORD);
controller.Control(RCCar.eDirection.LEFT);
controller.Control(RCCar.eDirection.RIGHT);
}
}
Action<T>대리자 사용
public class Unit
{
public enum eUnitType { MARINE, FIREBAT, MEDIC }
public Unit(eUnitType unit)
{
Console.WriteLine("{0}이 생성되었습니다.", unit);
}
}
public class Barracks
{
public delegate void DelCreate(Unit.eUnitType marine);
private Unit unit;
private DelCreate delCreate;
public Barracks()
{
}
public void CreateUnit(Unit.eUnitType unit, Action<Unit> callback)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("{0}0%", i);
Thread.Sleep(500);
}
callback(new Unit(unit));
}
}
public class App
{
public App()
{
Barracks barracks = new Barracks();
barracks.CreateUnit(Unit.eUnitType.MARINE, (unit) =>
{
Console.WriteLine("마린이 생성되었습니다.");
});
}
}
Func ★★★★
public class App
{
public App()
{
Func<int,Bag> createBag = (id) =>
{
return new Bag(id);
};
var bag = createBag(100);
Console.WriteLine(bag);
Player player = new Player();
player.Init(bag);
ItemData data = new ItemData(100, "SCAR-L");
Item item = new Item(data);
Func<Item, ItemData> lookItem = (item) =>
{
return item.Data;
};
ItemData itemData = lookItem(item);
Console.WriteLine(itemData.id, itemData.name);
}
}
public class Bag
{
public Bag(int id)
{ }
public void CreateBag()
{
Console.WriteLine("가방이 생성되었습니다.");
}
}
public class Player
{
public Player()
{
}
public void Init(Bag bag)
{
Console.WriteLine("플레이어가 가방을 챙겼습니다.");
}
}
public class ItemData
{
public int id;
public string name;
public ItemData(int id, string name)
{
this.id = id;
this.name = name;
}
}
public class Item
{
private ItemData data;
public Item(ItemData data)
{
this.data = data;
}
public ItemData Data()
{
return this.data;
}
}
다중 메서드 호출하기
public class App
{
public App()
{
this.Print(this.GetLength, "안녕하세요");
}
private void Print(Func<string, int> func, string msg)
{
//대리자 호출
int length = func(msg);
Console.WriteLine(length);
}
private int GetLength(string str)
{
return str.Length;
}
}
메서드의 매개변수로 람다 식 전달
public class App
{
public App()
{
var result = this.Calc(3, 5,(x, y)=>3 + 4);
Console.WriteLine(result);
}
public int Calc(int x, int y, Func<int, int, int> calc)
{
return calc(x, y);
}
}
Comparison대리자
public class Temperature
{
private double kelvin;
public double Kelvin
{
get
{
return this.kelvin;
}
set
{
if (value < 0)
{
throw new ArgumentException("Temperature cannot be less than absolute zero.");
}
else
{
this.kelvin = value;
}
}
}
public Temperature(double kelvin)
{
this.kelvin = kelvin;
}
public int CompareTo(Temperature other)
{
if (other == null) return 1;
return this.kelvin.CompareTo(other.kelvin);
}
}
'C# > 수업내용' 카테고리의 다른 글
0325 싱글턴 패턴 (0) | 2021.03.25 |
---|---|
0324 File 클래스 (외부 파일 불러오기) (0) | 2021.03.24 |
0323 대리자를 이용해 다중 메서드 호출 Multicast delegate/delegate chaining (0) | 2021.03.23 |
0323 익명 함수 (0) | 2021.03.23 |
0322 대리자 delegate★★ (0) | 2021.03.22 |