用Interface关键字定义的数据类型,接口 定义一系列属性,事件,方法,而不提供任何实现。如果实现,必须有派生类实现。注:属性于事件可以看作特殊的方法。
接口的继承:
接口实现:类继承接口,类必须实现该接口的列出的成员;
接口的继承:接口继承,将多个接口组合在一起来创建新的功能。
创建一个IShapeObj接口,里面定义两个方法area和show。Circle,Square,Rectangle三个继承于此接口:

1 namespace Test
2 {
3 interface IShapeObj
4 {
5 void area();
6 void show();
7 }
8 class Circle : IShapeObj
9 {
10 public void area()
11 {
12 Console.WriteLine("Circle的area()方法:");
13 }
14 public void show()
15 {
16 Console.WriteLine("Circle的show()方法:");
17 }
18 }
19 class Square : IShapeObj
20 {
21 public void area()
22 {
23 Console.WriteLine("Square的area()方法:");
24
25 }
26 public void show()
27 {
28 Console.WriteLine("Square的show()方法:");
29 }
30 }
31 class Rectangle : IShapeObj
32 {
33 public void area()
34 {
35 Console.WriteLine("Rectangle的area()方法:");
36
37 }
38 public void show()
39 {
40 Console.WriteLine("Rectangle的show()方法:");
41 }
42 }
43 class Program
44 {
45 static void Main(string[] args)
46 {
47 IShapeObj[] shapeobj = new IShapeObj[3];
48 shapeobj[0] = new Circle();
49 shapeobj[1] = new Square();
50 shapeobj[2] = new Rectangle();
51 shapeobj[0].area();
52 shapeobj[0].show();
53 shapeobj[1].area();
54 shapeobj[1].show();
55 shapeobj[2].area();
56 shapeobj[2].show();
57
58
59
60 }
61 }
62 }
继承于多个接口:

1 public interface ISwitch1
2 {
3 void On();
4 }
5 public interface ISwitch2
6 {
7 void Off();
8 }
9 public abstract class Switch1
10 {
11 public abstract void On();
12 }
13 public abstract class Switch2
14 {
15 public abstract void Off();
16 }
派生类书写于调用方法:

1 public class B : Switch1, ISwitch1, ISwitch2
2 {
3 public override void On()
4 {
5 Console.WriteLine("B -- on");
6 //throw new NotImplementedException();
7 }
8 void ISwitch1.On()
9 {
10 Console.WriteLine("B -- ISwitch1 --- on");
11
12 }
13 public void Off()
14 {
15 Console.WriteLine("B -- ISwitch1 --- Off");
16 }
17 }
18
19 public class A : Switch1, ISwitch2
20 {
21 public override void On()
22 {
23 Console.WriteLine("A---ON()");
24 //throw new NotImplementedException();
25 }
26 public void Off()
27 {
28 Console.WriteLine("A--off");
29 }
30 }
31 static void Main(string[] args)
32 {
33
34 //A a = new A();
35 //a.On();
36 //a.Off();
37 //Switch1 s = new A();
38 //s.On();
39 //ISwitch2 i = new A();
40 //i.Off();
41 B b = new B();
42 b.Off();
43 b.On();
44
45 }
怀揣着一点点梦想的年轻人
相信技术和创新的力量
喜欢快速反应的工作节奏
相信技术和创新的力量
喜欢快速反应的工作节奏