namespace ConsoleApplication4 { class dongwu { public void youyong() { Console.WriteLine("She was splendid like our flagship!"); } public void quanji() { Console.WriteLine("But it's all in the past!"); } public void tiaowu() { Console.WriteLine("Target Yamato!"); } } }
namespace ConsoleApplication4 { class dog:dongwu,Interface1,Interface2 { void Interface1.changge() { Console.WriteLine("She never gave up the hope even till the end!"); } void Interface2.changge() { Console.WriteLine("Only the sea knows!"); } } }
namespace ConsoleApplication4 { class Program { static void Main(string[] args) { dog data = new dog(); Interface1 idata = new dog();//接口不能建立自己的实例,可以通过子类新建自己的实例 dog data1 = new dog(); Interface1 idata1 = data1;//接口可以实现基类的类型相互转换 data1 = (dog)idata1; Interface1 idatai = new dog();//显示接口,接口中同名函数区分:子类中“Interface.方法名()”区分 Interface2 idataii = new dog(); idatai.changge();//显示接口的方法必须通过接口类型实例调用 idataii.changge(); idatai.tiaowu();//接口中同名函数不区分方法默认为所有接口中都是同一个方法 idataii.tiaowu(); Console.ReadLine(); } } }