内测------------使用多态描述动物的运动

来源:互联网 发布:剑网三藏剑男神脸数据 编辑:程序博客网 时间:2024/05/04 18:05

使用多态描述动物的运动

一、语言和环境

A、实现语言

 C#

B、环境要求

 Visual Studio 2012

二、功能要求

在森林中生活着很多小动物,现创建控制台程序来描述各种动物的运动。如图-3所示。

                  图-3 抽象方法实现多态

要求:

1、  不同的动物都有执行运动的功能。

2、  3种不同动物对象保存在一个泛型集合中。

3、  不能使用判断语句判断动物类型。

4、  使用抽象方法实现不同动物的多态,不可以使用方法的重载。

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 多态描述动物运动{    class Program    {        static void Main(string[] args)        {            List<Animal> list = new List<Animal>();            Console.WriteLine("森林里的小动物快乐的生活着!");            list.Add(new Fish());            list.Add(new Dog());            list.Add(new Birds());            foreach (Animal item in list)            {                item.Action();            }            Console.ReadLine();        }    }}

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 多态描述动物运动{    public class Fish:Animal    {        public override void Action()        {            Console.WriteLine("鱼儿在水里游");        }    }}

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 多态描述动物运动{    public class Dog:Animal    {        public override void Action()        {            Console.WriteLine("狗在地上跑!");        }    }}

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 多态描述动物运动{    public class Birds:Animal    {        public override void Action()        {            Console.WriteLine("鸟儿在天上飞!");        }    }}

0 0