C#学习笔记-铁锰C#入门 视频P29 接口 例子三(手机接口 实现三个手机与方法 解耦)

// 铁锰C#入门 视频P29 接口 例子三(手机接口 实现三个手机与方法 解耦)

// 在代码当中,若有可以替换的地方,就一定会有接口存在 为了松耦合 解耦而生,,使的功能提供方可替换 降低风险

1
using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApp24 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 var uSERp = new PhoneUser(new sums()); 13 uSERp.UserPHONE(); 14 Console.ReadKey(); 15 } 16 } 17 18 class PhoneUser 19 { 20 private Iphone _Iphone; //此时我们的字段不是某一款具体类型的手机了 而是 接口类型的 21 public PhoneUser(Iphone iphone) 22 { 23 _Iphone = iphone; 24 } 25 26 public void UserPHONE() 27 { 28 _Iphone.Dail(); 29 _Iphone.PickUp(); 30 _Iphone.Recive(); 31 _Iphone.Send(); 32 33 } 34 } 35 36 interface Iphone //定义一个接口 37 { //一下实现四种方法: 打电话 接电话 发短信 收短信 38 void Dail (); //方法体为空不可以省略大括号,抽象方法和接口方法不用大括号用分号 39 void PickUp(); 40 void Send(); 41 void Recive(); 42 //int Sdsd { get; set; } 43 } 44 45 class Nokia : Iphone //此时这边红的波浪线 点击下去选择“实现接口”即可 46 { 47 48 } 49 50 class moto : Iphone 51 { 52 public void Dail() 53 { 54 Console.WriteLine("moto dail!..."); 55 } 56 57 public void PickUp() 58 { 59 Console.WriteLine("moto pickup!"); 60 } 61 62 public void Recive() 63 { 64 Console.WriteLine("moto Recive"); 65 } 66 67 public void Send() 68 { 69 Console.WriteLine("moto Send!.."); 70 } 71 } 72 73 class sums : Iphone 74 { 75 public void Dail() 76 { 77 Console.WriteLine("sums dail!..."); 78 } 79 80 public void PickUp() 81 { 82 Console.WriteLine("sums pickup!"); 83 } 84 85 public void Recive() 86 { 87 Console.WriteLine("sums Recive"); 88 } 89 90 public void Send() 91 { 92 Console.WriteLine("sums Send!.."); 93 } 94 } 95 96 97 }

 

posted @ 2020-09-21 10:52  庚子年小菜鸟  阅读(138)  评论(0)    收藏  举报