编程论坛
注册
登录
编程论坛
→
C语言论坛
c#代码
w1908199591
发布于 2020-12-25 00:08, 960 次点击
12.面向对象编程:创建Console应用程序。在长方体类Cuboid中定义了计算体积方法Cubage();正方体Cube继承该类,使用虚方法Cubage 实现正方体体积的计算方法。
窗体的比较擅长这个题目都没看懂
1 回复
#2
apull
2020-12-25 01:02
大概就这意思吧。
程序代码:
using
System;
namespace
bccb
{
class
Cuboid
{
public
virtual
int
Cubage()
{
return
length * width * height;
}
public
void
seLength(
int
l)
{
length = l;
}
public
void
setWidth(
int
w)
{
width = w;
}
public
void
setHeight(
int
h)
{
height = h;
}
protected
int
length;
protected
int
width;
protected
int
height;
}
class
Cube: Cuboid
{
public
override
int
Cubage()
{
return
(width*width*width);
}
}
class
CubeTester
{
static
void
Main(
string
[] args)
{
Cuboid cuboid =
new
Cuboid();
cuboid.seLength(
5
);
cuboid.setWidth(
2
);
cuboid.setHeight(
3
);
Console.WriteLine(
"
长方体体积: {0}
"
, cuboid.Cubage());
Cube cube =
new
Cube();
cube.setWidth(
5
);
Console.WriteLine(
"
正方体体积: {0}
"
, cube.Cubage());
Console.ReadKey();
}
}
}
[此贴子已经被作者于2020-12-25 01:05编辑过]
1