接口

C#接口实例详解(1):http://blog.sina.com.cn/s/blog_574c993d0100d59n.html

C#接口实例详解(2):http://blog.sina.com.cn/s/blog_574c993d0100d59i.html

根据课本写的接口程序:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 using System.Collections;
 8 
 9 namespace 数据结构_Product
10 {
11     class First_1
12     {
13         /*主方法Main*/
14         static void Main(string[] args)
15         {
16             #region 接口的基本知识
17             //IBook ib = null;
18 
19             //ib = new NewBook("《程序员的SQL金典》", "杨中科", 200);
20 
21             //ib.ShowBook();
22 
23             NewBook newbook = new NewBook("《程序员的SQL金典》", "杨中科", 200);
24 
25             newbook.ShowBook();
26 
27             Console.ReadKey();
28             #endregion
29         }
30 
31         #region 接Main方法中的程序
32         /*接口*/
33         public interface IBook
34         {
35             //string GetTitle();
36             //int GetPages();
37             //void SetPages(int pages);
38             void ShowBook();
39         }
40 
41         /*实现接口的类*/
42         public class NewBook : IBook
43         {
44             /*定义的字段*/
45             public string title;
46             public int pages;
47             public string author;
48 
49             /*构造函数*/
50             public NewBook(string _title, string _author, int _pages)
51             {
52                 this.title = _title;
53                 this.author = _author;
54                 this.pages = _pages;
55             }
56 
57             ///*实现接口中的方法GetTitle*/
58             //public string GetTitle()
59             //{
60             //    return title;
61             //}
62 
63             ///*实现接口中的方法GetPages*/
64             //public int GetPages()
65             //{
66             //    return pages;
67             //}
68 
69             ///*实现接口中的方法SetPages*/
70             //public void SetPages(int pages)
71             //{
72             //    this.pages = pages;
73             //}
74 
75             /*实现接口中的方法ShowBook*/
76             public void ShowBook()
77             {
78                 Console.WriteLine("Title:{0}", title);
79                 Console.WriteLine("Author:{0}", author);
80                 Console.WriteLine("Pages:{0}", pages);
81             }
82         }
83         #endregion
84     }
85 }

*其中main方法中的注释部分,是另一种实例化的方法,用哪一个都能运行。

接口类中的注释部分注释掉程序也可以运行,不注释也可以运行。

原文地址:https://www.cnblogs.com/KTblog/p/4270018.html