【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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萌狼蓝天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值