c# 学习 实现多个接口

多个接口间用,号分开即可,如

  1 /*
2 Example8_2.cs illustrates implementing multiple interfaces
3  */
4
5 using System;
6
7
8 // define the IDrivable interface
9 public interface IDrivable
10 {
11
12 // method declarations
13 void Start();
14 void Stop();
15
16 // property declaration
17 bool Started
18 {
19 get;
20 }
21
22 }
23
24
25 // define the ISteerable interface
26 public interface ISteerable
27 {
28
29 // method declarations
30 void TurnLeft();
31 void TurnRight();
32
33 }
34
35
36 // Car class implements the IMovable interface
37 public class Car : IDrivable, ISteerable
38 {
39
40 // declare the underlying field used by the
41 // Started property of the IDrivable interface
42 private bool started = false;
43
44 // implement the Start() method of the IDrivable interface
45 public void Start()
46 {
47 Console.WriteLine("car started");
48 started = true;
49 }
50
51 // implement the Stop() methodof the IDrivable interface
52 public void Stop()
53 {
54 Console.WriteLine("car stopped");
55 started = false;
56 }
57
58 // implement the Started property of the IDrivable interface
59 public bool Started
60 {
61 get
62 {
63 return started;
64 }
65 }
66
67 // implement the TurnLeft() method of the ISteerable interface
68 public void TurnLeft()
69 {
70 Console.WriteLine("car turning left");
71 }
72
73 // implement the TurnRight() method of the ISteerable interface
74 public void TurnRight()
75 {
76 Console.WriteLine("car turning right");
77 }
78
79 }
80
81
82 class Example8_2
83 {
84
85 public static void Main()
86 {
87
88 // create a Car object
89 Car myCar = new Car();
90
91 // call myCar.Start()
92 Console.WriteLine("Calling myCar.Start()");
93 myCar.Start();
94
95 // call myCar.TurnLeft()
96 Console.WriteLine("Calling myCar.TurnLeft()");
97 myCar.TurnLeft();
98
99 }
100
101 }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值