C#接口理解应用,接口回调

2019-04-26  本文已影响0人  青九子
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace Main

{

 

    class Program

    {

        static void Main(string[] args)

        {

            USB usb=new USB();

            USBInter usbInter = usb;

            PC pc = new PC();

            pc.ConnectUSB(usbInter,"USB3.0");

            bool state=pc.write("ABC");

            if (state)

            {

                //Console.WriteLine(USB.USB_MEMARY);

                Console.WriteLine(pc.read());

            }

            Console.Read();

        }

    }

    interface USBInter

    {

        bool write(string data);

        string read();

    }

    class USB : USBInter

    {

        public static string USB_TYPE = "USB2.0";

        public static string USB_MEMARY;

        #region USBInter 成员

        public bool write(string data)

        {

            if (data == null || data.Length == 0 || data == "")

            {

                return false;

            }

            else

            {

                USB_MEMARY = data;

                return true;

            }

        }

        public string read()

        {

            if (USB_MEMARY == null || USB_MEMARY == "" || USB_MEMARY.Length == 0)

            {

                Console.WriteLine("数据读取失败...");

                return null;

            }

            else

            {

                Console.WriteLine("数据读取成功...");

                return USB_MEMARY;

            }

        }

        #endregion

    }

    class PC : USBInter

    {

        private USBInter usbInter;

        public bool ConnectUSB(USBInter usbInter,string uType)

        {

            if (uType == "USB3.0" || uType == "USB2.0")

            {

                this.usbInter = usbInter;

                Console.WriteLine(uType+"已成功连接...");

                return true;

            }

            else

            {

                return false;

            }

        }

        #region USBInter 成员

        public bool write(string data)

        {

            if (data == null || data.Length == 0 || data == "")

            {

                Console.WriteLine("数据写入失败...");

                return false;

            }

            else

            {

                usbInter.write(data);

                Console.WriteLine(  "数据写入成功...");

                return true;

            }

        }

        public string read()

        {

                return usbInter.read();

        }

        #endregion

    }

}

上一篇 下一篇

猜你喜欢

热点阅读