<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      《C#入門詳解》劉老師 接口,依賴反轉,單元測試

      接口,依賴反轉,單元測試

      一、IEnumerable接口使用

       1 using System;
       2 using System.Collections;
       3 using System.Collections.Generic;
       4 using System.Linq;
       5 using System.Text;
       6 using System.Threading.Tasks;
       7 
       8 namespace IEnumerable接口使用
       9 {
      10     class Program
      11     {
      12         static void Main(string[] args)
      13         {
      14             int[] nums1 = new int[] { 1, 2, 3, 4, 5 };
      15             ArrayList nums2 = new ArrayList { 1, 2, 3, 4, 5 };
      16             Console.WriteLine(sum(nums2));
      17             Console.WriteLine(Avg(nums2));
      18             Console.WriteLine(sum(nums1));
      19             Console.WriteLine(Avg(nums1));
      20         }
      21         //供方為整形數組和Arraylist    供方Array實現了IEnumerable接口,Arraylist也實現了IEnumerable這個接口,Array和Arraylist都遵守這個鍥約保證自己可以被迭代
      22         //需方為Sum  和  Avg這兩個函數   ,需方我只要求你傳進來的參數可以被迭代就行了,因為我要用foreach循環去迭代里面的每一個整數
      23 
      24         //供方需方都遵守IEnumerable接口這個鍥約
      25         static int sum(IEnumerable nums)  //int[] nums     ;;  ArrayList nums(object類型的所以要加int)
      26         {
      27             int sum = 0;
      28             foreach (var n in nums) sum += (int )n;
      29             return sum;
      30         }
      31 
      32         static double Avg(IEnumerable nums)
      33         {
      34             int sum = 0;
      35             double count = 0;
      36             foreach( var n in nums) { sum +=(int) n;count++; }
      37             return sum / count;
      38         }
      39 
      40     }
      41 }

       二、接口(供需雙方都滿足的鍥約)

      類與類之間,對象與對象之間分工合作,面向對象世界里有一個術語叫做依賴,服務的提供者和服務的使用者之間有一個依賴關系。實現接口,必須要把接口里的所有成員全部實現。

       1 /*========人和手機之間是解耦的============*/
       2 
       3 using System;
       4 using System.Collections.Generic;
       5 using System.Linq;
       6 using System.Text;
       7 using System.Threading.Tasks;
       8 
       9 namespace Interface_Example
      10 {
      11     class Program
      12     {
      13         static void Main(string[] args)
      14         {
      15             // IPhone V = new NokiaPhone();實現接口的實例
      16             var user = new PhoneUSER(new NokiaPhone());      //(V);
      17             user.usephone();
      18         }
      19     }
      20 
      21 
      22     class PhoneUSER
      23     {
      24         private IPhone _phone;
      25         public PhoneUSER (IPhone phone)
      26         {
      27             _phone = phone;
      28         }
      29         public void usephone()
      30         {
      31             _phone.Dial();
      32             _phone.PickUP();
      33             _phone.Send();
      34             _phone.Receive();
      35 
      36         }
      37 
      38 
      39     }
      40 
      41 
      42 
      43     interface IPhone
      44     {
      45         void Dial();
      46         void PickUP();
      47         void Send();
      48         void Receive();
      49 
      50     }
      51 
      52     class NokiaPhone : IPhone
      53     {
      54         public void Dial()
      55         {
      56             Console.WriteLine("Nokia calling...");
      57         }
      58 
      59         public void PickUP()
      60         {
      61             Console.WriteLine("Hello,this is tim");
      62         }
      63 
      64         public void Receive()
      65         {
      66             Console.WriteLine("Nokia message ring...");
      67         }
      68 
      69         public void Send()
      70         {
      71             Console.WriteLine("hello");
      72         }
      73     }
      74 
      75     class EricssonPhone : IPhone
      76     {
      77         public void Dial()
      78         {
      79             Console.WriteLine("EricssonPhone calling...");
      80         }
      81 
      82         public void PickUP()
      83         {
      84             Console.WriteLine("hi,this is tim");
      85         }
      86 
      87         public void Receive()
      88         {
      89             Console.WriteLine("Ericsson message ring...");
      90         }
      91 
      92         public void Send()
      93         {
      94             Console.WriteLine("good evening");
      95         }
      96     }
      97 
      98 
      99 }
      View Code

      人和手機之間是解耦的。

      三、依賴反轉

      人和手機之間是解耦的。什么是解耦?解耦在代碼中的表現就是依賴反轉(依賴倒置)。單元測試就是依賴反轉在開發中的直接應用。

       1 /*============強耦合,依賴關系,電扇依賴電源=======*/
       2 using System;
       3 
       4 namespace InterfaceExample
       5 {
       6 
       7     class Program
       8       {
       9        var fan = new DeskFan(new PowerSupply());
      10        Console.Writeline(fan.Work());
      11        }
      12     class PowerSupply
      13       {
      14         public int GetPower()
      15           {
      16             return 100;
      17            }
      18        }
      19 
      20     class DeskFan
      21       {
      22         private PowerSupply _powerSupply;
      23         public DeskFan(PowerSupply powerSupply)
      24           {
      25              _powerSupply = powerSupply;
      26            }
      27          public string Work()
      28            {
      29             int power = _powerSupply.GetPower();
      30             if (power<=0)
      31             {return "won't work!!!!";}
      32             else if(power<100)
      33             {return "slow!!!!";}
      34              else if(power<200)
      35              {return "work fine!!!!!!";}
      36              else
      37              {return "warning!!!!!!";}
      38              } 
      39        }
      40 
      41 }

       

       

       

       1 /*===================解耦==================*/
       2 using System;
       3 
       4 namespace InterfaceExample
       5 {
       6 
       7     class Program
       8       {
       9        var fan = new DeskFan(new PowerSupply());
      10        Console.Writeline(fan.Work());
      11 
      12        }
      13 
      14     InterFace IPowerSupply
      15       { int GetPower();}
      16 
      17 
      18     class PowerSupply : IPowerSupply
      19       {
      20         public int GetPower()
      21           {
      22             return 100;
      23            }
      24        }
      25 
      26     class DeskFan
      27       {
      28         private IPowerSupply _powerSupply;
      29         public DeskFan(IPowerSupply powerSupply)
      30           {
      31              _powerSupply = powerSupply;
      32            }
      33          public string Work()
      34            {
      35             int power = _powerSupply.GetPower();
      36             if (power<=0)
      37             {return "won't work!!!!";}
      38             else if(power<100)
      39             {return "slow!!!!";}
      40              else if(power<200)
      41              {return "work fine!!!!!!";}
      42              else
      43              {return "warning!!!!!!";}
      44 
      45              } 
      46 
      47 
      48        }
      49 
      50 
      51 
      52 }

      四、單元測試

       

             

       

      posted @ 2020-09-27 23:34  遙想公瑾  閱讀(228)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 久久综合精品成人一本| 日韩精品亚洲专区在线观看| 中文字日产幕码三区国产| 制服丝袜美腿一区二区| 成人亚洲性情网站www在线观看| 特级做a爰片毛片免费看无码 | 人妻少妇精品无码专区| 免费人成网站视频在线观看| 午夜国人精品av免费看| 成年女人午夜毛片免费视频| 国产欧美亚洲精品第一页在线| 激情综合网激情五月激情| 99久久久国产精品免费无卡顿 | 欧美成人精品在线| 大余县| 亚洲AV无码不卡在线播放| 国产偷国产偷亚洲清高APP| 国产午夜亚洲精品国产成人| 男女裸交免费无遮挡全过程| 性欧美VIDEOFREE高清大喷水 | 国产欧美精品一区二区三区| 国产真实younv在线| 九九热免费在线观看视频| 小污女小欲女导航| 久久亚洲精品亚洲人av| 国产女人喷潮视频在线观看| 国产国语一级毛片| 91精品久久一区二区三区| 亚洲aⅴ无码专区在线观看春色| 国产高清自产拍av在线| 无码人妻一区二区三区线| 亚洲夂夂婷婷色拍ww47| 额济纳旗| 18禁免费无码无遮挡网站| 日韩精品亚洲专在线电影| 国产精品永久在线观看| 下面一进一出好爽视频| 中文有无人妻vs无码人妻激烈| 成人3D动漫一区二区三区| 亚洲国产精品毛片在线看| 最新日韩精品中文字幕|