C#/C#언어

0324 _ 복습 //

minquu 2021. 3. 24. 12:19
반응형


멀티캐스트 대리자

 

대리자 하나에 여러개 메소드를 넣는다.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study12
{
    //대리자 선언
    public delegate void GoHome();
    class CarDriver
    {
        public static void GoLeft()
        {
            Console.WriteLine("좌회전");
        }
        public static void GoRignt()
        {
            Console.WriteLine("우회전");
        }
        public static void GoForward()
        {
            Console.WriteLine("앞으로");
        }
    }
    public class App
    {
        public App()
        {
            //대리자 초기화
            GoHome go = new GoHome(CarDriver.GoLeft);  //대리자 안에는 메서드 들어가야함
            go += new GoHome(CarDriver.GoRignt);
            go += new GoHome(CarDriver.GoForward);

            go();
        }
    }
}

 

 

CarDriver.GoLeft  // CarDriver 의 형식을 받아서 하는 것 이기 떄문에. GoLeft 메소드는 static이 되어야한다. ★

 

//멀티캐스트 대리자 // 중간에 마이크 짤리는 것

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study12
{
    //대리자 선언
    public delegate void GoHome();
    public delegate void Say(string message);
    //class CarDriver
    //{
    //    public static void GoLeft()
    //    {
    //        Console.WriteLine("좌회전");
    //    }
    //    public static void GoRignt()
    //    {
    //        Console.WriteLine("우회전");
    //    }
    //    public static void GoForward()
    //    {
    //        Console.WriteLine("앞으로");
    //    }
    //}
    public class Mic {
        public bool micState;
        public Mic(bool micState)
        {
            this.micState = micState;
        }
        public void Speak(string message) {
            if (this.micState == true)
            {
                Console.WriteLine(message);
                this.micState = false;
            }
            else {
                Console.WriteLine("{0}{1}...{2}{3}{4}...",
        message[0], message[1], message[2], message[3], message[4]);
            }

        }
    }

    public class App
    {
        public App()
        {
            ////대리자 초기화
            //GoHome go = new GoHome(CarDriver.GoLeft);  //대리자 안에는 메서드 들어가야함
            //go += new GoHome(CarDriver.GoRignt);
            //go += new GoHome(CarDriver.GoForward);
            //go -= new GoHome(CarDriver.GoRignt);

            //go();

            Mic mic1 = new Mic(true);
            Mic mic2 = new Mic(false);
            Say say = new Say(mic1.Speak);
            say += new Say(mic2.Speak);

            say("안녕하세요");


        }
    }
}

---- > 선생님 방법 // 문자열 짜르는 거 참고하기

 

    public class Mic {
        public int Id { get; private set; }
        //생성자 
        public Mic(int id) {
            this.Id = id;
        }

        public void Speak(string message) {
            if (this.Id == 100)
            {
                Console.WriteLine(message);
            }
            else if(this.Id == 102)
            {
                var str1 = message.Substring(0, 2);
                var str2 = message.Substring(2);
                message = string.Format("{0}...{1}..", str1, str2);
                Console.WriteLine(message);
            }
        }
    }

    public class App
    {
        public App()
        {
            Console.WriteLine("App");

            //대리자 초기화 
            GoHome go = new GoHome(CarDriver.GoLeft);
            go += new GoHome(CarDriver.GoRight);
            go += new GoHome(CarDriver.GoForward);
            go -= new GoHome(CarDriver.GoRight);
            go();

            Mic mic1 = new Mic(100);
            Mic mic2 = new Mic(102);
            Say say = new Say(mic1.Speak);
            say += new Say(mic2.Speak);

            say("안녕하세요.");
        }
    }
}

 

------

익명함수 (람다)

무명메서드

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study12
{
    //대리자 선언
    public delegate void DelPrint(string msg);
    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 delprint = new DelPrint(printer.Print);
            delprint(message);

            DelPrint delPrint2 = delegate (string msg) {
                Console.WriteLine("무명메서드");
                Console.WriteLine(msg);
            };
            delPrint2(message);

        }
    }
}

이름이 없는 (대리자의) 대리자에 바로 메서드 기능을 만들어 넣는다.

 

----

람다

-- 매개 변수가 있는 람다식 -- 

            DelPrint delPrint3 = (msg) =>
            {
                Console.WriteLine("람다식");
                Console.WriteLine(msg);
            };

            //대리자 호출
            delPrint3(message);

----

드론

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace study12
{   //Drone
    public class Drone {
        public enum eDirection { 
            FORWARD, BACKWORD, LEFT, RIGHT
        }
        public void Move(eDirection dir) {
            Console.WriteLine("{0}로 이동합니다.", dir);
        }
    }
    public class RemoteControler {
        public delegate void DelMove(Drone.eDirection dir);
        private Drone drone;
        private DelMove delMove;
        public RemoteControler()
        {

        }
        public void Connect(Drone drone) {
            this.drone = drone;
            this.delMove = new DelMove(this.drone.Move);
            Console.WriteLine("{0}와 연결되었습니다.", this.drone);
        }
        public void Control(Drone.eDirection dir)
        {
            this.delMove(dir);
        }
    }
    public class App
    {
        public App()
        {
            Drone drone = new Drone();
            RemoteControler remoteControler = new RemoteControler();
            remoteControler.Connect(drone);
            //drone.Move(Drone.eDirection.FORWARD);
            remoteControler.Control(Drone.eDirection.LEFT);
            remoteControler.Control(Drone.eDirection.BACKWORD);
        }


    }
}

 


Func, 

 

Action 

Predicate 

Comparison

event 

EventHandler 

 

반응형