步步为营-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

转载于:https://www.cnblogs.com/YK2012/p/6701930.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值