我想在一个单独的类中的方法为我做一些数学,然后将结果写入控制台。我现在遇到的问题是,它说对象引用没有要使用的实例。我以为我在类中早些时候实例化了调用所有其他方法的方法,但显然有些东西是不正确的,我不知道该怎么做才能使它工作。数学的第二部分会给我同样的错误,但是如果我能解决这个问题,我应该能够轻松地修复第二部分。类的实例不存在?
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();
将使其所需的实例,但显然不是?
我做了改变,但现在它告诉我“'苹果':成员名称不能与他们的封闭类型相同”在苹果类....> _>我觉得自己像一个总的菜鸟 – user1787114
@alexeilevenkov感谢编辑 – Andy
你不能有一个方法,它是类的名字 – Andy