作为一个专业的程序员用csharp定义一个名为Cylinder的圆柱体类其字段数据为底面半径和高计算其体积、表面积。定义类变量字段为radius height接着定义两个方法计算体积和表面积再用Volume和Area两个方法名标识然后用构造方法对对象进行构造再定义一个新的类Test类对Cylinder类对象进行测试最后完成整个代码。
代码如下:
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 著作权归作者所有。请勿转载和采集!