.创建一个收费接口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();
}
}
}