c#简单类的继承

时间: 1ms        内存:128M

描述:

编写代码实现:定义了三个类Bird、Mapie、Eagle。其中Bird为抽象类,定义了一个抽象方法Eat()。Mapie类和Eagle类为Bird的派生类。Mapie类中重写了Eat()方法,重载了一个Eat(int time)方法。Eagle类中也重写了Eat()方法。

输入:

输入time参数的值

输出:

各个方法的名称

示例输入:

10

示例输出:

Mapie eat!
Mapie eat 10!
Eagle eat!
Eagle eat!

提示:

参考答案(内存最优[4472]):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{

    abstract class Bird { public  abstract void eat();}


    class Mapie : Bird {
        public override void eat() { Console.WriteLine("Mapie eat!"); }
        public void eat(int time) {  Console.WriteLine("Mapie eat "+time+"!") ;}

    }

    class Eagle : Bird {
        public override void eat() { Console.WriteLine("Eagle eat!"); }
    }













    class Program
    {
        static void Main(string[] args)
        {
            int z = Convert.ToInt32(Console.ReadLine());
            Mapie m = new Mapie();
            m.eat();
            m.eat(z);
            Eagle e = new Eagle();
            e.eat();
            e.eat();
            Console.ReadKey();
        }
    }
}

参考答案(时间最优[28]):

using System;
  
namespace ex3_5
{
      
    class Program
    {
        abstract class Bird
        {
            abstract public void Eat();
        }
        class Mapie :Bird
        {
            public override void Eat()
            {
                Console.WriteLine("Mapie eat!");
            }
            public void Eat(int time)
            {
                Console.WriteLine("Mapie eat {0}!",time);
            }
        }
        class Eagle : Bird
        {
            public override void Eat()
            {
                Console.WriteLine("Eagle eat!");
            }
        }
        static void Main(string[] args)
        {
            Bird c ;
            Mapie m = new Mapie();
            Eagle e = new Eagle();
              
            string input = Console.ReadLine(); 
            int time = Convert.ToInt32(input); //输入10
            c = m;
              
            c.Eat();
            m.Eat(time);
            e.Eat();
            c = e;
            c.Eat();
        }
    }
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。