C#程序设计实验报告面向对象程序设计(实验1第三题)附源码

课程名称 C#程序设计

实验名称 实验一

叁、第三题

一、实验题目

编写一个矩形类(Rect)与一个圆类(Circle),分别通过构造方法对一个矩形对象(rect1)与一个圆对象(circle1)进行初始化后,求出矩形与圆的面积。

二、实验要求

同时具有如下功能:可以设置和读取矩形的边长和圆的半径,但只能读取它们的面积,不能修改面积。

三、实验代码以及执行结果

1、Rect类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 第三题
{
    class Rect
    {
        private double length; //矩形的长
        private double width;  //矩形的宽

        public Rect(double length,double width)
        {
            this.length = length;
            this.width = width;
        }

        public double Length { get => length; set => length = value; }
        public double Width { get => width; set => width = value; }

        public double getArea()     //获取矩形的面积
        {
            return length * width;
        }
    }
}

2、Circle类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace 第三题
{
    class Circle
    {
        const double PI = 3.1415;
        private double radius;   //圆的半径

        public Circle(double radius)
        {
            this.radius = radius;
        }

        public double Radius { get => radius; set => radius = value; }

        public double getArea()     //获取圆的面积
        {
            return radius * radius * PI;
        }
        
    }
}

3、主类:
using System;

namespace 第三题
{
    class Program
    {
        static void Main(string[] args)
        {
            Rect rect1 = new Rect(2, 4);
            Console.WriteLine("react1的面积为:{0}",rect1.getArea());
            rect1.Length = 4;
            rect1.Width = 8;
            Console.WriteLine("react1的面积为:{0}", rect1.getArea());
            Circle circle1 = new Circle(2);
            Console.WriteLine("circle1的面积为:{0}", circle1.getArea());
            circle1.Radius = 4;
            Console.WriteLine("circle1的面积为:{0}", circle1.getArea());
            
        }
    }
}

4、执行结果:

在这里插入图片描述

四、实验总结

通过这次实验,我学会了如何快速生成属性的get set 方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

别卷了,球球了。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值