using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication12 { class Program { static void Main(string[] args) { Mp3 m = new Mp3(); //因为两接口中成员名字相同,不能直接访问到。所以把对象转换为接口 //再访问对应接口中定义的方法和属性。 //通过把对象转换为不同的接口访问同名的方法,实现了对方法多态的访问。 IUsb iu = (IUsb)m; Console.WriteLine(iu.MaxSpeed); Console.WriteLine(iu.TransData()); IBlueTooth ib = (IBlueTooth)m; Console.WriteLine(ib.MaxSpeed); Console.WriteLine(ib.TransData()); Console.WriteLine(m.Play()); Console.ReadLine(); } } //接口中的成员不能加访问限定符,如public等 public interface IUsb { int MaxSpeed { get; } string TransData(); } public interface IBlueTooth { int MaxSpeed { get; } string TransData(); } //Mp3类从IUsb和IBlueTooth继承 public class Mp3 : IUsb, IBlueTooth { //因为两接口中的成员名字相同,所以实现时加接口名限定 int IUsb.MaxSpeed { get { return 1024; } } string IUsb.TransData() { return "USB Transdata..."; } int IBlueTooth.MaxSpeed { get { return 48; } } string IBlueTooth.TransData() { return "BlueTooth TransData..."; } public string Play() { return "Playing..."; } } }