【发布时间】:2023-03-14 10:33:01
【问题描述】:
这是我的代码:
class Program
{
abstract class Shape
{
public abstract double GetArea();
}
class Rectangle : Shape
{
double length = 100.57;
double width = 100.14;
public override double GetArea()
{
double result = length * width;
return result;
}
}
class Circle : Shape
{
double radius = 50.34;
public override double GetArea()
{
double result = Math.PI * Math.Pow(radius, 2);
return result;
}
}
static void Main(string[] args)
{
}
}
为什么它不返回任何东西?我想要我的 GetArea() 方法中的长度 * 宽度的结果以及圆的面积。我尝试了返回然后结果但是如果我启动应用程序没有任何反应。我也试过这样做:
static void Main(string[] args)
{
GetArea();
}
但这只是红色下划线。
【问题讨论】:
-
你的意思是
new Rectangle().GetArea()? -
@canton7 我不知道该说什么我只想从我的 Rectangle 中获得长度 * 宽度的区域,然后在控制台中将其打印为双精度。
-
你实际上并没有调用你这里写的代码,它只是进入
Main方法,什么都不做然后退出。 -
我怎么称呼它? @大卫G
-
就像canton说的,你需要为你的每个类创建一个实例并调用
GetArea方法。