0324 복습

2021. 3. 24. 16:53C#/수업내용

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);
        }
    }