1. 程式人生 > >C#深度學習の接口傳參(interface)-----接口的妙用

C#深度學習の接口傳參(interface)-----接口的妙用

廣度 view style 中間 rgs conn span main 接口

一、接口可以幹嘛

我們知道,接口的本質是一個約束規範,委托是方法縱向(形式上)的封裝,接口是不同方法橫向(廣度)的封裝

接口中不能有變量,但是可以有屬性方法。常見的,我們可以用接口:

1、實現需求方的方法

2、接口作為參數,實現對不同類的解耦,下面是常見的男女類

技術分享圖片
    public interface ISay
    {
        void Say();
    }

    public class Man:ISay
    {
        public void Say()
        {
            Console.WriteLine("你好,我是男士!
"); } } public class Woman : ISay { public void Say() { Console.WriteLine("你好,我是女士!"); } } public class Peole { public void Say(ISay iPeople) { iPeople.Say(); } } class Program {
static void Main(string[] args) { Man man = new Man(); Woman woman = new Woman(); Peole peole = new Peole(); peole.Say(man); peole.Say(woman); Console.ReadLine(); } }
View Code

3、接口作返回值,返回一個實現了接口的對象,基本和上面的例子類似

二、接口可以傳參(常用來做對外SDK)

下面以兩種常見構型講解

1、A工廠生產產品,B工廠代理,C工廠賣,於是我們:

    public interface ITransfer
    {
        void Transfer(string msg);
    }

    public class FactoryA
    {
        public ITransfer FacATransfer = null;

        public FactoryA()
        {
            product();
        }

        public void product()
        {
            int produceNum=0;
   
            Task.Factory.StartNew(()=> 
            {
                while (true)
                {
                    produceNum++;
                    FacATransfer?.Transfer(String.Format("來自FactoryA的第{0}個產品", produceNum));
                    Thread.Sleep(2000);
                }
            });
        }

    }

    public class FactoryB //中間商
    {
        public void ConnectInit(ITransfer transfer)
        {
            FactoryA factoryA = new FactoryA();
            factoryA.FacATransfer = transfer;
        }

    }

    public class FactoryC:ITransfer
    {
        public FactoryC()
        {
            FactoryB factoryB = new FactoryB();
            factoryB.ConnectInit(this);
        }
        public void Transfer(string msg)
        {
            Console.WriteLine("為FactoryC生產,"+msg);
        }
    }

調用

    class Program
    {
        static void Main(string[] args)
        {
            FactoryC factoryC = new FactoryC();
            //FactoryD factoryD = new FactoryD();
            Console.ReadLine();
        }
    }

2、工廠A生產,D直接代理

    public class FactoryA
    {
        public ITransfer FacATransfer = null;

        public FactoryA()
        {
            product();
        }

        public void product()
        {
            int produceNum=0;
   
            Task.Factory.StartNew(()=> 
            {
                while (true)
                {
                    produceNum++;
                    FacATransfer?.Transfer(String.Format("來自FactoryA的第{0}個產品", produceNum));
                    Thread.Sleep(2000);
                }
            });
        }

    }

調用:

    class Program
    {
        static void Main(string[] args)
        {
            //FactoryC factoryC = new FactoryC();
            FactoryD factoryD = new FactoryD();
            Console.ReadLine();
        }
    }

學習請加qq群:568055323

C#深度學習の接口傳參(interface)-----接口的妙用