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. 运行结果: