{"action":"opened","issue":{"url":"https://api.github.com/repos/Damon-Salvatore/CSharp-Learning/issues/10","repository_url":"https://api.github.com/repos/Damon-Salvatore/CSharp-Learning","labels_url":"https://api.github.com/repos/Damon-Salvatore/CSharp-Learning/issues/10/labels{/name}","comments_url":"https://api.github.com/repos/Damon-Salvatore/CSharp-Learning/issues/10/comments","events_url":"https://api.github.com/repos/Damon-Salvatore/CSharp-Learning/issues/10/events","html_url":"https://github.com/Damon-Salvatore/CSharp-Learning/issues/10","id":448897367,"node_id":"MDU6SXNzdWU0NDg4OTczNjc=","number":10,"title":"接口与多态","user":{"login":"Damon-Salvatore","id":28801735,"node_id":"MDQ6VXNlcjI4ODAxNzM1","avatar_url":"https://avatars0.githubusercontent.com/u/28801735?v=4","gravatar_id":"","url":"https://api.github.com/users/Damon-Salvatore","html_url":"https://github.com/Damon-Salvatore","followers_url":"https://api.github.com/users/Damon-Salvatore/followers","following_url":"https://api.github.com/users/Damon-Salvatore/following{/other_user}","gists_url":"https://api.github.com/users/Damon-Salvatore/gists{/gist_id}","starred_url":"https://api.github.com/users/Damon-Salvatore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Damon-Salvatore/subscriptions","organizations_url":"https://api.github.com/users/Damon-Salvatore/orgs","repos_url":"https://api.github.com/users/Damon-Salvatore/repos","events_url":"https://api.github.com/users/Damon-Salvatore/events{/privacy}","received_events_url":"https://api.github.com/users/Damon-Salvatore/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2019-05-27T14:52:57Z","updated_at":"2019-05-27T14:52:57Z","closed_at":null,"author_association":"OWNER","body":"### 接口和多态\r\n\r\n#### 1. .NET 中接口的定义规范\r\n\r\n- a.使用关键字 `interface` 定义，接口类名称通常使用`I`开头。\r\n\r\n- b.接口中的属性、方法等，只是做一个声明，而没有任何实现。\r\n\r\n- c.接口中的属性、方法等，默认都是 `public`,不要画蛇添足。\r\n\r\n#### 2.接口的特点\r\n\r\n- a.接口具有强制性，实现接口的类必须实现接口的所有成员。\r\n\r\n- b.一个类既可以实现多个接口，也可以同时继承其它类。\r\n\r\n#### 3.提高团队成员并行开发项目的效率\r\n\r\n- a.接口使用者只关心接口的应用功能，而不关心接口的实现细节。\r\n\r\n- b.接口的实现者只关心接口如何实现的内部细节，而不关心谁使用。\r\n\r\n#### 4.接口的应用场合\r\n\r\n- a.如果某一个功能点需求变化较多，应使用接口保证系统的可扩展性。\r\n\r\n- b.如果想实现团队成员的并行开发，可以使用接口来规范对象的使用。\r\n\r\n#### 5.接口和抽象类的比较\r\n\r\n|        |                 抽象类                 |             接口             |\r\n| ------ | :------------------------------------: | :--------------------------: |\r\n| 不同点 |            用`abstract`定义            |      用`interface`定义       |\r\n|        |             只能继承一个类             |       可以实现多个接口       |\r\n|        |      非抽象派生类必须实现抽象方法      | 实现接口的类必须实现所有成员 |\r\n|        |       需要`override`实现抽象方法       |           直接实现           |\r\n| 相似点 |            都不能直接实例化            |\r\n|        |           都包含未实现的方法           |\r\n|        | 子类或“接口实现类”必须实现未实现的方法 |\r\n\r\n#### 6. demo\r\n\r\n`IMultiPrint.cs`\r\n\r\n```cs\r\n/// <summary>\r\n/// 多功能打印机接口\r\n/// </summary>\r\npublic interface IMultiPrinter\r\n{\r\n  // 打印\r\n  void Print(string contents);\r\n  //复印\r\n  void Copy(string contents);\r\n  // 传真\r\n  bool Fax(string contents);\r\n}\r\n```\r\n\r\n然后分别定义三个具体的类实现`IMultiPrint`接口：\r\n\r\n`CanonPrinter.cs`\r\n\r\n```cs\r\npublic class CanonPrinter : IMultiPrinter\r\n{\r\n  public void Print(string contents)\r\n  {\r\n    //在这里编写打印程序...\r\n    Console.WriteLine(\"佳能打印机正在打印：\" + contents);\r\n  }\r\n\r\n  public void Copy(string contents)\r\n  {\r\n    //在这里编写复印程序...\r\n    Console.WriteLine(\"佳能打印机正在复印：\" + contents);\r\n  }\r\n\r\n  public bool Fax(string contents)\r\n  {\r\n    //在这里编写传真程序...\r\n    Console.WriteLine(\"佳能打印机正在传真：\" + contents);\r\n    return true;\r\n  }\r\n}\r\n```\r\n\r\n`EpsonMultiPrinter.cs`\r\n\r\n```cs\r\npublic  class EpsonMultiPrinter:IMultiPrinter\r\n{\r\n  public void Print(string contents)\r\n  {\r\n    //在这里编写打印程序...\r\n    Console.WriteLine(\"欢迎使用Epson打印机！\");\r\n    Console.WriteLine(\"Epson打印机正在打印：\" + contents);\r\n  }\r\n\r\n  public void Copy(string contents)\r\n  {\r\n    //在这里编写复印程序...\r\n    Console.WriteLine(\"欢迎使用Epson打印机！\");\r\n    Console.WriteLine(\"Epson打印机正在复印：\" + contents);\r\n  }\r\n\r\n  public bool Fax(string contents)\r\n  {\r\n    //在这里编写传真程序...\r\n    Console.WriteLine(\"欢迎使用Epson打印机！\");\r\n    Console.WriteLine(\"Epson打印机正在传真：\" + contents);\r\n    return true;\r\n  }\r\n}\r\n```\r\n\r\n`HPMultiPrinter.cs`\r\n\r\n```cs\r\nclass HPMultiPrinter : IMultiPrinter\r\n{\r\n  public void Print(string contents)\r\n  {\r\n    //在这里编写打印程序...\r\n    Console.WriteLine(\"欢迎使用HP打印机！\");\r\n    Console.WriteLine(\"HP打印机正在打印：\" + contents);\r\n  }\r\n\r\n  public void Copy(string contents)\r\n  {\r\n    //在这里编写复印程序...\r\n    Console.WriteLine(\"欢迎使用HP打印机！\");\r\n    Console.WriteLine(\"HP打印机正在复印：\" + contents);\r\n  }\r\n\r\n  public bool Fax(string contents)\r\n  {\r\n    //在这里编写传真程序...\r\n    Console.WriteLine(\"欢迎使用HP打印机！\");\r\n    Console.WriteLine(\"HP打印机正在传真：\" + contents);\r\n    return true;\r\n  }\r\n}\r\n```\r\n\r\n`Program.cs`中实现多态\r\n\r\n```cs\r\nclass Program\r\n{\r\n  static void Main(string[] args)\r\n  {\r\n    Print(new HPMultiPrinter());\r\n    Console.WriteLine(\"-----------------------------------------\");\r\n    Print(new CanonPrinter());\r\n    Console.ReadLine();\r\n  }\r\n\r\n  //多态特性（接口作为方法的返回值、接口作为方法的参数）\r\n  static void Print(IMultiPrinter printer)\r\n  {\r\n    printer.Print(\"个人信息表\");\r\n    printer.Copy(\"个人信息\");\r\n    printer.Fax(\"身份证\");\r\n  }\r\n}\r\n```\r\n"}}
{"actor":{"display_login":"Damon-Salvatore"}}