c#接口的用法

来源:互联网 发布:软件测试就业怎么样 编辑:程序博客网 时间:2024/06/16 08:54
namespace 抽象类借口{    class Program    {        class Chinese:Walkable        {            public void Walk()            {                Console.WriteLine("pia pia");            }        }        class Dog:Walkable        {            public void Walk()            {                Console.WriteLine("Dog走路");            }        }        class Bird : Flyable,Walkable//可以继承多个接口        {            public void Fly()            {                Console.WriteLine("小鸟飞");            }            public void Walk()            {                Console.WriteLine("Bird走路  ");            }        }        interface Flyable//接口定义 定义一个飞的行为 不能定义实现        {            void Fly();//在接口里定义方法不需要 修饰符              }        interface Walkable        {            void Walk();        }      /* public abstract class Person//抽象类person 只要有一个方法标志为abstract,类就要标志为abstract        {            public abstract void SayHello();//抽象类方法        }       public class Chinese : Person       {           public override void SayHello()           {               Console.WriteLine("我是中国人");           }       }*/        static void Main(string[] args)        {            Bird b = new Bird();            b.Fly();            Walkable w = new Dog();            w.Walk();            w = new Chinese();            w.Walk();            Dog dog=(Dog)w; //Dog dog =w as Dog;                        /*Person p = new Person();//抽象类是不能被new出来的            Chinese c1 = new Chinese();            c1.SayHello();*/            Console.ReadKey();        }    }}
C#里的接口 可以多个继承 与类 的继承有所区别。
原创粉丝点击