//(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。
//(2)编写一个类,继承自矩形类,长方体,具有长、宽、高属性,和计算体积的方法。
//(2)编写一个类,继承自矩形类,长方体,具有长、宽、高属性,和计算体积的方法。
//(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。
class Rectangle
{
public float wide;
public float length;
public Rectangle(float wide,float length) {
this.wide = wide;
this.length = length;
}
public float GetArea() {
float S = wide * length;
return S;
}
}
class Cub : Rectangle {
public float height;
public Cub(float wide, float length,float height) :base(wide,length) {
this.height = height;
}
public float GetArea()
{
float S = wide * length;
return S;
}
public float GetVolume() {
float V = wide * length * height;
return V;
}
}
测试类
static void Main(string[] args)
{
Rectangle f = new Rectangle(2,3);
Console.WriteLine(f.GetArea());
Cub g = new Cub(2,3,4);
Console.WriteLine(g.GetArea());
Console.WriteLine(g.GetVolume());
}
测试结果

这篇博客探讨了C#中的继承概念,并通过创建一个长方体类来演示。文章提供了一个测试类的示例,该类用于初始化长方体的长、宽、高属性,并计算并输出其底面积和体积。
1639

被折叠的 条评论
为什么被折叠?



