C#可用interface IDrivable实现接口:
1 /*
2 Example8_1.cs illustrates 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 // Car class implements the IDrivable interface
26 public class Car : IDrivable
27 {
28
29 // declare the underlying field used by the Started property
30 private bool started = false;
31
32 // implement the Start() method
33 public void Start()
34 {
35 Console.WriteLine("car started");
36 started = true;
37 }
38
39 // implement the Stop() method
40 public void Stop()
41 {
42 Console.WriteLine("car stopped");
43 started = false;
44 }
45
46 // implement the Started property
47 public bool Started
48 {
49 get
50 {
51 return started;
52 }
53 }
54
55 }
56
57
58 class Example8_1
59 {
60
61 public static void Main()
62 {
63
64 // create a Car object
65 Car myCar = new Car();
66
67 // call myCar.Start()
68 myCar.Start();
69 Console.WriteLine("myCar.Started = " + myCar.Started);
70
71 // call myCar.Stop()
72 myCar.Stop();
73 Console.WriteLine("myCar.Started = " + myCar.Started);
74
75 }
76
77 }