【发布时间】:2014-12-26 16:15:38
【问题描述】:
我正在尝试编写一个有两个类的程序,并从一个到另一个调用 2 个变量,但我得到两个错误,说“'Area.Circle' 不包含'result1 的定义'" 和 "'Area.Circle' 不包含 'result2' 的定义"。我该如何解决这个问题?
using System;
namespace Area
{
class Circle
{
public static void Area()
{
Console.WriteLine("Enter the radius of the first circle: ");
int r1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the radius of the second circle: ");
int r2 = Convert.ToInt32(Console.ReadLine());
double pi = Math.PI;
double result1 = pi * r1 * r1;
double result2 = pi * r2 * r2;
Console.WriteLine("The area of the first circle is {0}\nThe area of the second circle is {1}\n", result1, result2);
}
}
class Minimum
{
static void Main(string[] args)
{
Circle.Area();
Circle one = new Circle();
double min = Math.Min(Circle.result1, Circle.result2);
Console.WriteLine("min");
}
}
}
【问题讨论】:
-
您在方法中定义了 result1 和 result2。这意味着这些变量是该方法的本地变量,不能在其外部调用。如果你想保留这些变量,你需要在类级别(在 Circle 内部但在 Area 外部)声明它们。
标签: c# .net class oop variables