2012-11-10 36 views
3

我想在一个单独的类中的方法为我做一些数学,然后将结果写入控制台。我现在遇到的问题是,它说对象引用没有要使用的实例。我以为我在类中早些时候实例化了调用所有其他方法的方法,但显然有些东西是不正确的,我不知道该怎么做才能使它工作。数学的第二部分会给我同样的错误,但是如果我能解决这个问题,我应该能够轻松地修复第二部分。类的实例不存在?

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();将使其所需的实例,但显然不是?

回答

3

MakeFruits方法中,您声明了两个变量,它们是您的MakeFruits()方法的本地变量,因此EatFruits()不能访问它们。

注意this.

public void MakeFruits() 
{ 
    this.apple = new Apple(); // "this." is written to make it clearer. 
    this.apple.apple(1.5); // let's not skip the steps 
    this.banana = new Banana(); 
    this.banana.banana(3.5); 
} 

因为你在MakeFruits()方法中声明本地水果类的属性applebanana住宿为null

在另一种情况下,您的apple()方法未正确注册半径。

public void SetRadius (double radius) 
{ 
    Radius = radius; // by Alexei 
} 

在任何情况下,如果你仍然不确定,看看Mauris' crash course notes on Pastebin:应该按如下方式写入。

+0

我做了改变,但现在它告诉我“'苹果':成员名称不能与他们的封闭类型相同”在苹果类....> _>我觉得自己像一个总的菜鸟 – user1787114

+0

@alexeilevenkov感谢编辑 – Andy

+1

你不能有一个方法,它是类的名字 – Andy

2

通过使用

Apple apple = new Apple(); 

你作用域这个版本的苹果到MakeFruits方法。因此,在您的EatFruits方法中,您可以访问该范围可用的苹果版本,该版本是未初始化的Apple apple。当你尝试访问成员时,你会得到一个错误,因为它没有被初始化。

我在这里看到的主要问题是范围和一些小姐使用案例。

class Apple : Fruit 
{ 
public double Radius { get;set;} 

//public void apple(double radius)//Constructors need to share the same case 
           //as their parent and inherently have no return value 
public Apple(double radius) 
{ 
    //Radius = Radius;//This is a self reference 
    Radius = radius;//This will reference the local variable to Apple, Radius 
} 
} 

同样的问题也出现在主程序

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();//You have already declared apple in this scope 
    //apple.apple(1.5);//This is redundant, what you most likely want is to have this done in a constructor 
    apple = new Apple(1.5);//this accesses the scoped apple, and uses the Apple constructor 
    //Banana banana = new Banana();//same scopeing issue as apple 
    banana = new Banana(); 
    banana.banana(3.5);//the banana class was not shown so I will leave this 
} 
public void EatFruits() 
{ 
    double dblpercent; 
    MakeFruits();//now made properly with scope 
    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);//Eat was never shown 
    Console.Write("% of your apple, and "); 
    banana.Eat(dblpercent);//Eat was never shown 
    Console.Write("% of your banana left."); 
    Console.ReadLine(); 
} 
} 
0

你只需要知道你的类上下文之间的区别,当你使用:

public void MakeFruits() 
{ 
    Apple apple = new Apple(); 
    apple.apple(1.5); 
    Banana banana = new Banana(); 
    banana.banana(3.5); 
} 

您告诉编译器“apple”和“banana”是局部变量,.....属于仅仅属于“MakeFruits()”方法的变量,你需要做的是使用关键词“this”,编译器会知道你需要instantia在您的类定义变量。

public void MakeFruits() 
{ 
    this.apple = new Apple(); 
    apple.apple(1.5); 
    this.banana = new Banana(); 
    banana.banana(3.5); 
} 
0

首先,您需要修复Apple的构造函数。

class Apple : Fruit 
{ 
    public double Radius { get;set;} 

    public Apple(double radius) 
    { 
     Radius = radius; 
    } 

} 

上面的代码说明了创建对象的正确方法。

你可能要考虑做这样的事情:

class Program 
{ 

    private static FruitGarden myGarden; 

    static void Main(string[] args) 
    { 
     //get a new garden 

     myGarden = new FruitGarden(); 
     Console.WriteLine(myGarden.PlantFruit("banana")); 
     //returns "You grew one banana!" 
     Console.WriteLine(myGarden.PlantFruit("apple")); 
     //returns "You grew one apple!" 
     Console.WriteLine(myGarden.PlantFruit("orange")); 
     //returns "You can't grow that type of fruit!" 

     EatFruits(); 
    } 

    static void EatFruits() 
    { 
     //Now, let's just iterate through our fruit garden and eat all of that 
     //yummy fruit! 
     for (int i = 0; i < myGarden.Fruits.Count; i++) 
     { 
      Fruit myFruitSnack = myGarden.Fruits[i]; 
      while (myFruitSnack.FruitSize > 0) 
      { 
       Console.WriteLine(myFruitSnack.TakeBite()); 
      } 
     } 

     Console.ReadLine(); 
    } 

} 

//We could make this virtual or an interface, but I'll leave that out for now. 
public class Fruit 
{ 

    private int fruitSize; 

    public int FruitSize 
    { 
     get 
     { 
      return this.fruitSize; 
     } 
    } 

    public Fruit(int size) 
    { 
     this.fruitSize = size; 
    } 

    //The size of the fruit is an internal property. You can see how 
    //big it is, of course, but you can't magically make a fruit smaller 
    //or larger unless you interact with it in a way that is allowable 
    //according to the current known laws of the universe and agriculture. 
    //I.E, you can take a bite :) 
    public string TakeBite() 
    { 
     if (this.fruitSize > 0) 
     { 
      this.fruitSize -= 1; //or any other value you decide to use 
     } 

     if (this.fruitSize > 0) 
     { 
      //again, there is so much more you can do here. 
      return "You take a bite of the fruit!"; 
     } 
     else 
     { 
      return "You take one more big bite and eat all of the fruit!"; 
     } 

    } 

} 

public class Apple : Fruit 
{ 
    //Someday, you might want to overload these... 
    public Apple(int fruitSize) 
     : base(fruitSize) 
    { 

    } 
} 

public class Banana : Fruit 
{ 
    //Someday, you might want to overload these... 
    public Banana(int fruitSize) 
     : base(fruitSize) 
    { 

    } 
} 

class FruitGarden 
{ 

    //Public property of FruitGarden that contains all of the fruits it has "grown." 
    public List<Fruit> Fruits { get; set; } 

    public FruitGarden() 
    { 
     //Instantiate your list now. 
     this.Fruits = new List<Fruit>(); 
    } 

    //There are better ways to do this, but for the sake of your project we're 
    //going to do something simple. We'll pass in a string representing the 
    //fruit type. 
    public string PlantFruit(string fruitType) 
    { 
     //We're going to implement a simple factory here. Google 'factory pattern' 
     //later and be prepared to spend a lot of time reading over the ideas 
     //you're going to run into. 
     switch (fruitType.ToLower()) 
     { 
      case "apple": 
       this.Fruits.Add(new Apple(10)); 
       break; 
      case "banana": 
       this.Fruits.Add(new Banana(5)); 
       break; 
      default: 
       return "You can't grow that type of fruit!"; 
     } 

     return "You grew one " + fruitType + "!"; 
    } 

} 

现在,请记住,这只是一个例子,掩盖了很多非常重要的概念。快乐的编码!