1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace 接口_隐式接口实现
7 {
8 // 定义接口1 : IChineseGreeting
9 interface IChineseGreeting
10 {
11 void SayHello();
12 }
13
14 // 定义接口2 : IAmericanGreeting
15 interface IAmericanGreeting
16 {
17 void SayHello();
18 }
19
20 // Speaker类实现两个接口: IChineseGreeting, IAmericanGreeting
21 public class Speaker : IChineseGreeting, IAmericanGreeting
22 {
23 // 隐式接口实现
24 public void SayHello()
25 {
26 Console.WriteLine("你好!");
27 }
28 }
29
30 class Program
31 {
32 static void Main(string[] args)
33 {
34 // 实例化Speaker
35 Speaker speaker = new Speaker();
36
37 // 调用中国人打招呼方式
38 IChineseGreeting iChineseG = (IChineseGreeting)speaker;
39 iChineseG.SayHello();
40
41 // 调用美国人打招呼方式
42 IAmericanGreeting iAmericanG = (IAmericanGreeting)speaker;
43 iAmericanG.SayHello();
44
45 /*------------------------------------------------------------------------------------------------------------
46 * 1. 最终的输出结果: 两个 "你好"
47 * 2. iChineseG/iAmericanG 调用了相同的SayHello()方法
48 ------------------------------------------------------------------------------------------------------------*/
49
50 Console.ReadLine();
51 }
52 }
53 }