【C#】【平时作业】习题-9-接口

1、什么是接口

为派生类提供因该遵守的标准结构,而本身只包含成员声明,不包含成员的定义

56FEA264024E4563DC9AB4BC099F3390.png

2、接口与抽象类有什么区别

70D996803F0BA17D006E79F8AA783B71.png

3、设计IBluetooth。

 public interface IBluetooth
    {
        //类型/版本  -- 萌狼蓝天 微信公众号:mllt9920
        double Type { get; }
        //最大传输速度  -- 萌狼蓝天 微信公众号:mllt9920
        double MaxTransferSpeed { get; }
        //传输数据  -- 萌狼蓝天 微信公众号:mllt9920
        string TransferDataByUsb(string data);
    }

4、设计Mobliephone,Laptop和SmallCar类,实现蓝牙接口。

 class Mobliephone : IBluetooth
    {
        public double Type { get=>3.0; }

        public double MaxTransferSpeed { get=>500; }

        public string TransferDataByBluetooth(string data)
        {
            return "MobilePhone using interface now!" + data;
        }
    }
 class Laptop : IBluetooth
    {
        public double Type { get => 2.5; }

        public double MaxTransferSpeed { get => 300; }

        public string TransferDataByBluetooth(string data)
        {
            return "Laptop using interface now!" + data;
        }
    }
 class SmallCar : IBluetooth
    {
        public double Type { get => 1.0; }

        public double MaxTransferSpeed { get => 100; }

        public string TransferDataByBluetooth(string data)
        {
            return "SmallCar using interface now!" + data;
        }
    }

5、设计方法UseDeviceBluetooth,输出显示接口中TransferDataByBluetooth方法的返回信息。

初级玩法

 private void button1_Click(object sender, EventArgs e)
        {
            Mobliephone m = new Mobliephone();
            MessageBox.Show(m.TransferDataByBluetooth("初级玩法"));
        }

中级玩法

 public void UseDeviceBluetooth(IBluetooth ib, string data)
        {
            string r = ib.TransferDataByBluetooth(data);
            MessageBox.Show(r);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Mobliephone m = new Mobliephone();
            Mobliephone l = new Mobliephone();
            UseDeviceBluetooth(m, "歌唱我的祖国");
            UseDeviceBluetooth(l, "妖精打架限定影像");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Mobliephone m = new Mobliephone();
            Laptop l = new Laptop();
            SmallCar c = new SmallCar();
            UseDeviceBluetooth(m, "歌唱我的祖国");
            UseDeviceBluetooth(l, "妖精打架限定影像");
            UseDeviceBluetooth(c, "开车车欸");
        }

软件界面

image.png

posted @ 2021-12-19 20:19  萌狼蓝天  阅读(165)  评论(0编辑  收藏  举报