1 /*接口的实现例子
2 * 2016年2月23日11:07:11
3 */
4 using System;
5 using System.Collections.Generic;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9
10 namespace Demo5
11 {
12 class Program
13 {
14 static void Main(string[] args)
15 {
16 //用接口实例化一个类对象
17 IFlyable sp = new SuperMan();
18 sp.Fly();
19 sp.Jump();
20 Console.ReadKey();
21 }
22 }
23
24 //接口声明用interface关键字
25 //方法可以没有任何实现,接口中只能包含方法
26 //接口中的成员不能带有任何修辞符,默认是public,但是不是写下去,会报错
27 public interface IFlyable
28 {
29 void Fly(); // 方法前不能带任何修辞符,默认是public
30 void Jump();
31 }
32
33 //定义一个类去实现接口
34 class SuperMan : IFlyable
35 {
36 //直接写方法,不需要使用override
37 public void Fly()
38 {
39 Console.WriteLine("Surperman can fly");
40 }
41
42 public void Jump()
43 {
44 Console.WriteLine("Surperman can jump");
45 }
46 }
47 }