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();会制作它需要的实例,但显然不是?

4

4 回答 4

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 在 Pastebin 上的速成课程笔记

于 2012-11-10T03:20:05.313 回答
2

通过使用

Apple apple = new Apple();

您已将此版本的 Apple 限定为该MakeFruits方法。因此,在您的EatFruits方法中,您可以访问该范围内可用的 apple 版本,这是一个未初始化的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();
 }
}
于 2012-11-10T03:28:01.367 回答
0

当您使用时,您只需要知道课堂上的上下文之间的区别:

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

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

public void MakeFruits()
{
    this.apple = new Apple();
    apple.apple(1.5);
    this.banana = new Banana();
    banana.banana(3.5);
}
于 2012-11-10T04:03:13.667 回答
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 + "!";
    }

}

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

于 2012-11-10T05:03:31.663 回答