C#接口的认识与分析

来源:互联网 发布:样本册设计软件 编辑:程序博客网 时间:2024/06/07 03:28
接口实现多态性——》源代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace The_001_Thread{    class Program    {        static void Main(string[] args)        {            IPersonable person = new Man();            IPersonable person1 = new Woman();            test(person);            test(person1);            Console.ReadKey();        }        static void test(IPersonable person)        {            person.say();        }    }    interface  IPersonable    {      void say();    }    class Man : IPersonable    {        public void say()         {            Console.WriteLine("男人");        }    }    class Woman : IPersonable    {        public void say()        {            Console.WriteLine("女人");        }    }}
输出结果:
从这个结果可以看得出来,通过接口实现了多态性,同时使该类多了一种能力。


0 0