C#接口范例

 1namespace ConsoleApplication1
 2{
 3    class Program
 4    {
 5        static void Main(string[] args)
 6        {
 7            USB usb;
 8            usb = new UDisk();//插入U盘
 9            usb.OutputFile();//从U盘读出文件
10            usb.InputFile();//往U盘写入文件
11            usb.Dispose();//拔出U盘
12            Console.WriteLine("");
13            usb = new MDisk();//插入移动硬盘
14            usb.OutputFile();//从移动硬盘读出文件
15            usb.InputFile();//往移动硬盘写入文件
16            usb.Dispose();//拔出移动硬盘
17            Console.WriteLine("");
18            usb = new MP4();//插入MP4
19            usb.OutputFile();//从MP4读出文件
20            usb.InputFile();//往MP4写入文件
21            usb.Dispose();//拔出MP4
22            Console.ReadKey();
23        }

24
25        //USB接口
26        public interface USB : IDisposable
27        {
28            void OutputFile();//读出文件
29            void InputFile();//写入文件
30        }

31
32        //U盘
33        public class UDisk : USB
34        {
35            public UDisk()
36            {
37                Console.WriteLine("U盘准备就绪");
38            }

39            public void OutputFile()
40            {
41                Console.WriteLine("从U盘读出文件");
42            }

43            public void InputFile()
44            {
45                Console.WriteLine("往U盘写入文件");
46            }

47            public void Dispose()
48            {
49                Console.WriteLine("U盘已被拔出");
50            }

51        }

52
53        //移动硬盘
54        public class MDisk : USB
55        {
56            public MDisk()
57            {
58                Console.WriteLine("移动硬盘准备就绪");
59            }

60            public void OutputFile()
61            {
62                Console.WriteLine("从移动硬盘读出文件");
63            }

64            public void InputFile()
65            {
66                Console.WriteLine("往移动硬盘写入文件");
67            }

68            public void Dispose()
69            {
70                Console.WriteLine("移动硬盘已被拔出");
71            }

72        }

73
74        //MP4
75        public class MP4 : USB
76        {
77            public MP4()
78            {
79                Console.WriteLine("MP4准备就绪");
80            }

81            public void OutputFile()
82            {
83                Console.WriteLine("从MP4读出文件");
84            }

85            public void InputFile()
86            {
87                Console.WriteLine("往MP4写入文件");
88            }

89            public void Dispose()
90            {
91                Console.WriteLine("MP4已被拔出");
92            }

93        }

94    }

95}
posted @ 2007-12-27 05:27  张远强  阅读(1090)  评论(9编辑  收藏  举报