首页 > 代码库 > 步步为营-04-一个通过接口实现多态的经典例子

步步为营-04-一个通过接口实现多态的经典例子

说明:涉及到继承,抽象类,虚方法,接口等知识点

鸟类都有翅膀,

鸟类都会吃,但吃的食物不一样

鸟类都会叫,虽然叫声不一样,喜鹊,燕子,麻雀,感觉都是叽叽喳喳的.程序员中的世界就是讲儿.

鸟类中,麻雀会飞,喜鹊会飞和鸵鸟不会飞

鸟类中,麻雀,喜鹊都是"叽叽喳喳"的叫,鸵鸟"哦哦"(当然我没有听过,也懒得去查,主要矛盾于次要矛盾)

另外飞机也会飞

定义一个父类-鸟类(属性:翅膀;方法:吃;虚方法:叫声)

定义一种能力(接口IFlyable) 

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bird
{
   public  interface IFly
    {
       void Fly();
    }
}
IFly
技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bird
{
    public abstract class Bird
    {
        //定义字段--翅膀
        private string wing;
        //根据字段生成属性
        public abstract string Wing
        {
            set;
            get;
        }
        //定义抽象方法--吃
        public abstract void Eat();
        //定义虚方法
        public virtual void Sing() {
            Console.WriteLine("叽叽渣渣");
        }

    }
}
Bird
技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bird
{
   public class Sparrow:Bird,IFly
    {
        public override void Eat()
        {
            Console.WriteLine("我是麻雀,我爱吃人类的粮食");
        }

        public void Fly()
        {
            Console.WriteLine("我会飞");
        }
        private string wing;
        public override string Wing
        {
            get
            {
                return wing; 
            }
            set
            {
                wing = "麻雀的翅膀";
            }
        }
    }
}
Sparrow
技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bird
{
    class Ostrich:Bird
    {
        private string wing;

        public override string Wing
        {
            get
            {
                return wing;
            }
            set
            {
                wing = "鸵鸟的翅膀";
            }
        }

        public override void Eat()
        {
            Console.WriteLine("我是鸵鸟,我吃人");

        }

        public override void Sing()
        {
            Console.WriteLine("哦!哦!");
        }
    }
}
Ostrich
技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bird
{
    public class Pie:Bird,IFly
    {
        private string wing;
        public override string Wing
        {
            get
            {
                return wing;
            }
            set
            {
                wing = "xique的翅膀";
            }
        }

        public override void Eat()
        {
            Console.WriteLine("我是喜鹊,谷类和虫子");
        }



        public void Fly()
        {
            Console.WriteLine("我是喜鹊,我会飞!");
        }
    }
}
View Code
技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bird
{
    class Program
    {
        static void Main(string[] args)
        {
            Bird[] birds = { new Sparrow (),new Ostrich(),new Pie()};
            foreach (Bird item in birds)
            {
                Console.WriteLine(item.Wing);
                item.Eat();
                item.Sing();
            }
           Sparrow sp =  new Sparrow ();
           Pie p = new Pie();
           sp.Fly();
           p.Fly();
           Console.Read();
        }
    }
}
Main

技术分享

步步为营-04-一个通过接口实现多态的经典例子