代码如下:

using System;

class Cylinder
{
    double radius;
    double height;

    public Cylinder(double r, double h)
    {
        radius = r;
        height = h;
    }

    public double Volume()
    {
        return Math.PI * Math.Pow(radius, 2) * height;
    }

    public double Area()
    {
        return 2 * Math.PI * radius * height + 2 * Math.PI * Math.Pow(radius, 2);
    }
}

class Test
{
    static void Main(string[] args)
    {
        Cylinder cylinder = new Cylinder(3, 5);
        Console.WriteLine("Cylinder volume: " + cylinder.Volume());
        Console.WriteLine("Cylinder area: " + cylinder.Area());
    }
}

在该代码中,我们定义了一个Cylinder类,它具有双精度浮点型radius和height字段,这些字段表示圆柱体的底面半径和高。我们还定义了两个方法:Volume()和Area(),它们用于计算圆柱体的体积和表面积。

在Test类中,我们创建了一个Cylinder对象,并打印出它的体积和表面积。通过这个例子,我们可以看到如何使用类和对象来计算圆柱体的体积和表面积。


原文地址: https://www.cveoy.top/t/topic/b4nG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录