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

实验4 面向对象编程(2)

【实验目的】
1.掌握类的继承和多态的使用。
2.掌握索引、委托、事件、接口、结构和枚举的使用。
【实验内容】

1、3453 简单类继承。编写代码实现:定义了三个类Bird、Mapie、Eagle。其中Bird为抽象类,定义了一个抽象方法Eat()。Mapie类和Eagle类为Bird的派生类。Mapie类中重写了Eat()方法,重载了一个Eat(int time)方法。Eagle类中也重写了Eat()方法。
在这里插入图片描述

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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string n=Console.ReadLine();
            int num;
            int.TryParse(n, out num);
            Mapie a=new Mapie();
            a.Eat();
            a.Eat(num);
            Eagle b = new Eagle();
            b.Eat();
            b.Eat();
            Console.ReadKey();


      
        }
        //自定义抽象类Bird
        public abstract class Bird{
            public abstract void Eat();
    }
        //派生类Mapie
          class Mapie : Bird {
            public override void Eat(){
                Console.WriteLine("Mapie eat!");
            }
            public void Eat(int time){
                Console.WriteLine("Mapie eat "+time+"!");
            }
        }
        //派生类Eagle
          class Eagle : Bird {
              public override void Eat()
              {
                  Console.WriteLine("Eagle eat!");
              }
          }
    }
}

2、3438 继承实例。计算长方形的面积。根据给出的代码,补全缺失的代码,输入两个数字为长方形的长和宽,从而得出长方形的面积。

样例代码:

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();
      }
   }
}


在这里插入图片描述
提交部分:

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

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

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);
            }
        }
    }
}

在这里插入图片描述

提交部分:

class Teacher {
        
        public void Register(Bell bell) {
        bell.Ring+=new DelegateRing(write);
        bell.OnRing();
        }
        public void write() { Console.WriteLine("teacher");}
    }
    class Student
    {

        public void Register(Bell bell)
        {
            bell.Ring += new DelegateRing(write1);
            bell.OnRing();
        }
        public void write1() { Console.WriteLine("student"); }
    }

参考:
C#委托和事件的使用示例
https://www.cnblogs.com/vickylinj/p/10922139.html
YTUOJ
https://www.imlhx.com/posts/7532.html

///至于为啥我也没看懂 这题期末着重复习一下

4、3485 接口实例。接口和类如下图所示,根据给出代码,补写缺失的代码,然后在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());
        }
    }
}

在这里插入图片描述
提交部分:

		public double demo1 { get; set; }
        public double demo2 { get; set; }
        public Rectangle(double w,double h){
            demo1 = w;
            demo2 = h;
        }
        public double Area() {
            double area1=0;
            if(demo1>0&&demo2>0)
            area1= demo1 * demo2;
            return area1;
        }
        public double Perimeter() {
            double pr1 = 0;
            if (demo1 > 0 && demo2 > 0)
                pr1 = (demo1 + demo2) * 2;
            return pr1;     
        }

///这里我只考虑了如果输入负数的处理,其实也没怎么处理,就是加了个if判断,但是也同样AC了

5、3440 多态实例。根据给出代码,补写缺失代码,当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();
      }
   }
}

在这里插入图片描述
提交部分:

     public int print(int a){
           int b;
           b = a * a * a;
           Console.WriteLine(b);
           return b;
       }
       public double print(double a) {
           double b;
           b = a * a;
           Console.WriteLine(b);
           return b;
       }
       public string print(string a) {
           string b;
           b = a;
           Console.WriteLine(b);
           return b;
       }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值