一个c#关于类的问题,自己做不出来了,指教

2025-05-13 15:36:50
推荐回答(1个)
回答1:

直接按题目的要求写就可以了,如下:

using System;

namespace ConsoleApplication1
{
    public class Rectangle
    {
        private double m_width, m_height;
        
        // 构造函数
        // width:宽
        // heigh:长(高)
        public Rectangle(double width, double height)
        {
            m_width = width;
            m_height = height;
        }
        
        // Area方法:计算面积
        public double Area()
        {
            return m_width * m_height;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Rectangle rect = new Rectangle(2.5, 1.5);
            Console.WriteLine("Area = {0}", rect.Area());

            Console.ReadKey();
        }
    }
}
// 输出
// Area = 3.75