由多个接口得到一个接口

简介: 接口间可用:实现服用,如public interface IMovable : IDrivable, ISteerable,例如 1 /* 2 Example8_6.cs illustrates deriving an 3 interface from multiple in...

接口间可用:实现服用,如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 }
相关文章
|
8月前
|
IDE C# 开发工具
C#系列之接口介绍
C#系列之接口介绍
|
8月前
|
C++
几个实用的C++函数接口
几个实用的C++函数接口
|
Java
接口2
接口2
79 1
|
Java
接口1
接口1
65 0
|
前端开发 Java
写一个接口该注意什么?
写一个接口该注意什么?
67 0
|
C# 索引
C#-接口
接口是一种用来定义程序的协议,它描述可属于任何类或结构的一组相关行为。接口可有方法、属性、事件和索引器或这四种成员的任何组合类型,但不能包含字段。接口只包含了成员的声明,在继承的类中进行实现。
118 0
|
Java 程序员 编译器
🛰️🛰️五、实现多个接口
🛰️🛰️五、实现多个接口
250 0
🛰️🛰️五、实现多个接口
|
JavaScript 前端开发 容器
56、GlobalEventHandlers 接口
某个对象的abort事件(停止加载)发生时,就会调用onabort属性指定的回调函数。 各种元素的停止加载事件,到底如何触发,目前并没有统一的规定。因此实际上,这个属性现在一般只用在<img>元素上面。
154 0