C# -- 接口 (關鍵字:interface)


C#: 接口(關鍵字:interface)

1.代碼(入門舉例)

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Console.WriteLine("-------------------------------------");
 6             IIntroduce iSE = new SoftwareEngineer();
 7             iSE.SayHi();
 8             iSE.DescribeMyself();
 9             iSE.SayGoodbye();
10 
11 
12             Console.WriteLine("-------------------------------------");
13             IIntroduce iTc = new Teacher();
14             iTc.SayHi();
15             iTc.DescribeMyself();
16             iTc.SayGoodbye();
17 
18             Console.ReadKey();
19         }
20     }
21 
22     interface IIntroduce
23     {
24         void SayHi();
25         void DescribeMyself();
26         void SayGoodbye();
27 
28     }
29 
30    class SoftwareEngineer : IIntroduce
31     {
32         public void DescribeMyself()
33         {
34             Console.WriteLine("I'm a software engineer !"); 
35         }
36 
37         public void SayGoodbye()
38         {
39             Console.WriteLine("Goodbye !");
40         }
41 
42         public void SayHi()
43         {
44             Console.WriteLine("Hi !");
45         }
46 
47     }
48 
49 
50    class Teacher : IIntroduce
51     {
52         public void DescribeMyself()
53         {
54             Console.WriteLine("I'm a Teacher !");
55         }
56         public void SayGoodbye()
57         {
58             Console.WriteLine("Goodbye !");
59         }
60         public void SayHi()
61         {
62             Console.WriteLine("Hi !");
63         }
64     }

2. 運行結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM