C#创建一个收费接口Charge,其中有一个方法charge();创建另一个收费接口Play,其中有一个方法play()。声明类Bus来实现接口Charge,对于Bus中的charge()实现为输出“

5 篇文章 0 订阅
2 篇文章 0 订阅

.创建一个收费接口Charge,其中有一个方法charge();创建另一个收费接口Play,其中有一个方法play()。

声明类Bus来实现接口Charge,对于Bus中的charge()实现为输出“公共汽车:1元/张,不计公里数”;

声明类Taxi来实现接口Charge,对于Taxi中的charge()实现为输出“出租车:1.6元/公里,起价5元3公里”;

声明类Cinema来同时实现Charge、Play,对于Cinema中的charge()实现为输出“电影院:30元/张,凭学生证享受半价”;对于Cinema中的play()实现输出为“正在放映电影”。

试编程实现类的设计并进行测试。

```c
namespace 
{
    public interface Charge   //声明接口Charge  
    {
        void charge();        //接口成员声明
    }
    public interface Play   //声明接口Play
    {
        void play();       //接口成员声明
    }
    public class Bus : Charge    // Bus 继承接口
    {
        public void charge()      //隐式实现
        {
            Console.WriteLine("公共汽车:1元/张,不计公里数");
        }

    }
    public class Taxi : Charge    // Taxi继承接口
    {
        public void charge()     // Taxi隐式实现
        {
            Console.WriteLine("出租车:1.6元/公里,起价5元3公里");
        }

    }
    public class Ciema : Charge, Play    // Ciema 继承接口 Charge, Play

    {
        public void charge()     // Ciema隐式实现
        {
            Console.WriteLine("电影院:30元/张,凭学生证享受半价");
        }
         void Play.play()     // Ciema显式接口成员实现,带有接口名前缀,不能使用public

        {
            Console.Write("正在放映电影");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Bus bus = new Bus();  //定义 Bus一个类实例
            bus.charge();
            Taxi taxi = new Taxi();
            taxi.charge();
            Ciema ciama = new Ciema();
            ciama.charge();
            Play p = (Play)ciama;    //定义一个接口实例 显示实现 需要
            p.play();
        }
    }
}

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值