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

使用多态描述动物的运动

一、语言和环境

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("鸟儿在天上飞!");
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值