C# 圆柱体类定义:计算体积和表面积
以下是 Cylinder 类的定义:
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 * radius * radius * height;
}
public double Area()
{
return 2 * Math.PI * radius * (radius + height);
}
}
以下是 Test 类的定义:
class Test
{
static void Main(string[] args)
{
Cylinder cylinder = new Cylinder(3.0, 5.0);
Console.WriteLine('底面半径为{0},高为{1}的圆柱体的体积为{2},表面积为{3}', cylinder.Radius, cylinder.Height, cylinder.Volume(), cylinder.Area());
}
}
其中,Cylinder 类的构造方法用于初始化对象的 radius 和 height 字段;Volume 方法用于计算圆柱体的体积,公式为πr²h;Area 方法用于计算圆柱体的表面积,公式为2πrh + 2πr²。Test 类的 Main 方法用于创建一个 Cylinder 对象并调用它的 Volume 和 Area 方法,输出圆柱体的体积和表面积。
原文地址: https://www.cveoy.top/t/topic/nomf 著作权归作者所有。请勿转载和采集!