以下是 Cylinder 类的代码:

public class Cylinder
{
    private double radius;
    private double height;

    public Cylinder(double radius, double height)
    {
        this.radius = radius;
        this.height = height;
    }

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

以下是 Test 类的代码:

public class Test
{
    public static void Main(string[] args)
    {
        Cylinder cylinder = new Cylinder(2.5, 5.0);

        Console.WriteLine('Radius: ' + cylinder.Radius);
        Console.WriteLine('Height: ' + cylinder.Height);
        Console.WriteLine('Volume: ' + cylinder.Volume());
        Console.WriteLine('Area: ' + cylinder.Area());
    }
}

在 Test 类中,我们创建了一个 Cylinder 对象,并调用了它的 Volume 和 Area 方法来计算它的体积和表面积。输出结果如下:

Radius: 2.5
Height: 5
Volume: 98.17477042468103
Area: 94.24777960769379

注意,在 Cylinder 类中,我们使用了 this 关键字来引用类内部的字段变量。在 Test 类中,我们通过 cylinder.Radius 和 cylinder.Height 来访问这些字段变量的值。


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

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