尔冬橙

博客园 首页 新随笔 联系 订阅 管理
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...";
        }
    }
}

 

posted on 2012-07-22 18:10  尔冬橙  阅读(214)  评论(0编辑  收藏  举报