2012-11-10 37 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 + "!"; 
    } 

} 

現在,請記住,這只是一個例子,掩蓋了很多非常重要的概念。快樂的編碼!