《.NET开发技术》实验04(计科) 面向对象编程(2)

问题 A: c#简单类的继承

题目描述

编写代码实现:定义了三个类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!

提示

注意格式!!!

using System;
namespace sample
{
    class Program
    {
        static void Main(string[] args)
        {
            int x=Convert.ToInt32(Console.ReadLine());
            Mapie r1 = new Mapie();
            Eagle r2 = new Eagle();
            r1.Eat();
            r1.Eat(x);
            r2.Eat();
            r2.Eat();
            Console.ReadKey();
        }
    }
    public abstract class Bird
    {
        public abstract void Eat();
    }
    public class Mapie : Bird {
        public override void Eat()
        {
            Console.WriteLine("Mapie eat!");
        }
        public void Eat(int time)
        {
            Console.WriteLine("Mapie eat "+time+"!");
        }
    }
    public class Eagle : Bird {
        public override void Eat()
        {
            Console.WriteLine("Eagle eat!");
        }
    }
}

问题 B: c#计算长方形的面积(继承问题)

题目描述

根据给出的代码,补全缺失的代码,输入两个数字为长方形的长和宽,从而得出长方形的面积。


using System;
namespace InheritanceApplication
{
   class Shape 
   {
      public void setWidth(int w)
      {
         width = w;
      }
      public void setHeight(int h)
      {
         height = h;
      }
      protected int width;
      protected int height;
   }
/****************/
编写此处代码,并只提交此处代码
/****************/
   
   class RectangleTester
   {
      static void Main(string[] args)
      {
         Rectangle Rect = new Rectangle();
        int width = int.Parse(Console.ReadLine());
          int height =int.Parse(Console.ReadLine());
         Rect.setWidth(width);
         Rect.setHeight(height);
         Console.WriteLine("总面积: {0}",  Rect.getArea());
         Console.ReadKey();
      }
   }
}

 

输入

3 5

输出

15

样例输入

5 7

样例输出

35
    class Rectangle:Shape
    {
        public int getArea()
        {
            return height*width;
        }
    }

问题 C: C#委托、类和事件的验证

题目描述

程序由两部分组成,如下代码所示。第一部分定义了委托、类和事件。第二部分进行验证。 

using System;
namespace HelloWorldApplication
{
    public delegate void DelegateRing();
    public class Bell{
        public event DelegateRing Ring;
        public void OnRing(){ Ring(); }
    }
/
            
            请填写代码

/
    class HelloWorld
    {
        static void Main(string[] args)
        {
            try{
                Teacher teacher = new Teacher();
                teacher.Register(new Bell());
                Student student = new Student();
                student.Register(new Bell());
                Console.ReadKey();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

输入

无输入

输出

验证事件输出

样例输入

样例输出

teacher
student

提示

只需要输出样例输出的结果

只需要提交需要填写的代码

    public class Teacher
    {
        internal void Register(Bell bell)
        {
            Console.WriteLine("teacher");
        }
    }
    public class Student
    {
        internal void Register(Bell bell)
        {
            Console.WriteLine("student");
        }
    }

问题 D: 接口实例(C#,IShape)

题目描述

接口实例。接口和类如下图所示,根据给出代码,补写缺失的代码,然后在Program类的静态Main方法中验证所实现的类。

using System;
namespace Myinterface
{
    public interface IShape
    {
        double Perimeter();
        double Area();
    }
    class Circle : IShape
    {
        public double Radius { get; set; }
        public Circle(double r)
        {
            Radius = r;
        }
        public double Area()
        {
            return Math.PI * Radius * Radius;
        }
        public double Perimeter()
        {
            return 2 * Math.PI * Radius;
        }
    }
    class Rectangle : IShape
    {
            /
            
            //请填写代码,实现输出矩形的面积和周长

            /
        
    }

    class Program
    {
        static void Main(string[] args)
        {
            double w, h;
            double.TryParse(Console.ReadLine(), out w);
            double.TryParse(Console.ReadLine(), out h);
            Rectangle r = new Rectangle(w, h);
            Console.WriteLine("area={0},Perimeter={1}",r.Area(), r.Perimeter());
        }
    }
}

输入

输入矩形长、高,如
10
3
 

输出

area=30,Perimeter=26

样例输入

10
3

样例输出

area=30,Perimeter=26

提示

需要考虑输入非数字、负数等

 public double Height { get;set;}
        public double Length { get; set; }
        public Rectangle(double h,double l)
        {
            Height = h;
            Length = l;
        }
        public double Area()
        {
            if (Height > 0 && Length > 0)
                return Height * Length;
            return 0;
        }
        public double Perimeter()
        {
            if (Height > 0 && Length > 0)
             return 2 * (Height + Length);
            return 0;
        }

问题 E: c#补充print(多态性问题)

题目描述

根据给出代码,补写缺失代码,当print函数内为整数的时候,输出整数的三次方,为浮点数,输出其二次方,为字符串时,直接输出。




using System;
namespace PolymorphismApplication
{
   class Printdata
   {
      /******************************************/
         在此处补写代码,并只提交此处的代码
   /******************************************/
      static void Main(string[] args)
      {
         Printdata p = new Printdata();
         
         p.print(2);
        
         p.print(1.23);
      
         p.print("Hello world");
         Console.ReadKey();
      }
   }
}
 

输入

 

输出

8
1.5129
Hello world

样例输出

8
1.5129
Hello world
          void print(int x)
          {
              Console.WriteLine(x * x * x);
          }
          void print(double x)
          {
              Console.WriteLine(x*x);
          }
          void print(string x)
          {
              Console.WriteLine(x);
          }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值