我正尝试让一个单独的类中的方法为我执行一些数学计算,并将结果写入控制台。现在我面临的问题是它显示对象引用没有可用的实例。我之前认为我已经在调用所有其他方法的类中实例化了它,但显然有些地方不对,我不知道该怎么做才能使其正常工作。第二部分的数学计算也会给我同样的错误,但如果我能解决这个问题,我应该能够轻松地解决第二个问题。
class FruitGarden
{
private Apple apple;
private Banana banana;
static void Main(string[] args)
{
FruitGarden fruitGarden = new FruitGarden();
fruitGarden.EatFruits();
}
public void MakeFruits()
{
Apple apple = new Apple();
apple.apple(1.5);
Banana banana = new Banana();
banana.banana(3.5);
}
public void EatFruits()
{
double dblpercent;
MakeFruits();
Console.WriteLine("You have an Apple and a Banana in your fruit garden.\n");
Console.WriteLine("What Percent of the Apple would you like to eat?");
dblpercent = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("\nWhat Percent of the Banana would you like to eat?");
dblpercent = Convert.ToDouble(Console.ReadLine());
Console.Write("You have ");
apple.Eat(dblpercent);
Console.Write("% of your apple, and ");
banana.Eat(dblpercent);
Console.Write("% of your banana left.");
Console.ReadLine();
}
}
它试图引用的苹果类是:
class Apple : Fruit
{
public double Radius { get;set;}
public void apple(double radius)
{
Radius = Radius;
}
}
我以为苹果
apple = new Apple();
会创建所需的实例,但显然并不是这样?
Radius=Radius
中确实没有SO... (而且我知道我当时在想什么-Radius {get;set{Raduis=value;}}
-为什么...我猜我需要更多的咖啡...) - Alexei Levenkov