接口间可用:实现服用,如public interface IMovable : IDrivable, ISteerable,例如
1 /*
2 Example8_6.cs illustrates deriving an
3 interface from multiple interfaces
4 */
5
6 using System;
7
8
9 // define the IDrivable interface
10 public interface IDrivable
11 {
12
13 // method declarations
14 void Start();
15 void Stop();
16
17 // property declaration
18 bool Started
19 {
20 get;
21 }
22
23 }
24
25
26 // define the ISteerable interface
27 public interface ISteerable
28 {
29
30 // method declarations
31 void TurnLeft();
32 void TurnRight();
33
34 }
35
36
37 // define the IMovable interface (derived from IDrivable and ISteerable)
38 public interface IMovable : IDrivable, ISteerable
39 {
40
41 // method declarations
42 void Accelerate();
43 void Brake();
44
45 }
46
47
48 // Car class implements the IMovable interface
49 public class Car : IMovable
50 {
51
52 // declare the underlying field used by the
53 // Started property of the IDrivable interface
54 private bool started = false;
55
56 // implement the Start() method of the IDrivable interface
57 public void Start()
58 {
59 Console.WriteLine("car started");
60 started = true;
61 }
62
63 // implement the Stop() methodof the IDrivable interface
64 public void Stop()
65 {
66 Console.WriteLine("car stopped");
67 started = false;
68 }
69
70 // implement the Started property of the IDrivable interface
71 public bool Started
72 {
73 get
74 {
75 return started;
76 }
77 }
78
79 // implement the TurnLeft() method of the ISteerable interface
80 public void TurnLeft()
81 {
82 Console.WriteLine("car turning left");
83 }
84
85 // implement the TurnRight() method of the ISteerable interface
86 public void TurnRight()
87 {
88 Console.WriteLine("car turning right");
89 }
90
91 // implement the Accelerate() method of the IMovable interface
92 public void Accelerate()
93 {
94 Console.WriteLine("car accelerating");
95 }
96
97 // implement the Brake() method of the IMovable interface
98 public void Brake()
99 {
100 Console.WriteLine("car braking");
101 }
102
103 }
104
105
106 class Example8_6
107 {
108
109 public static void Main()
110 {
111
112 // create a Car object
113 Car myCar = new Car();
114
115 // call myCar.Start()
116 Console.WriteLine("Calling myCar.Start()");
117 myCar.Start();
118
119 // call myCar.TurnLeft()
120 Console.WriteLine("Calling myCar.TurnLeft()");
121 myCar.TurnLeft();
122
123 // call myCar.Accelerate()
124 Console.WriteLine("Calling myCar.Accelerate()");
125 myCar.Accelerate();
126
127 }
128
129 }