更改GetOutput()方法,以便返回详细信息C#

时间:2015-05-29 19:58:20

标签: c#

这是一种不得已而为之的事情。在我的一个实验室里,我遇到了一个问题,我遇到了问题而且我无法理解它甚至理解它,因为我在C#上真的很糟糕! 无论如何,最初的问题是: 在课程DogChorus中,更改GetOutput()方法,以便返回所有创建的狗的详细信息,包括所有狗的腿数。有一个提示是'你需要调用你通过类名创建的静态NoOfLegs属性。 任何人可以给我的帮助/提示将不胜感激! 两个类的代码如下:

namespace HelloDogs
{
    class Dog
    {
        private string barkSound;
        private string breed;
        private int dogHeight;
        private static int noOfLegs;

        public static int NoOfLegs
        {
            get { return Dog.noOfLegs; }
            set { Dog.noOfLegs = value; }
        }

        public int DogHeight
        {
        get { return dogHeight; }
        set { dogHeight = value; }
        }
        private string dogColour;

        public string DogColour
        {
        get { return dogColour; }
        set { dogColour = value; }
        }

        public string Breed
        {
            get { return breed; }
            set { breed = value; }
        }
        private string dogSpeech;

        public Dog()
        {
            barkSound = "Woof!";
            breed = "cocker spaniel";
            dogHeight = 25;
            dogColour = " White";
        }

     private bool isBig (int y)
        {
            int dogHeight = y;
            if(dogHeight <50)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

     public string GetSpeech(int theDog)
     {
         if (isBig(theDog))
         {

             dogSpeech = "Hello. I am a " + breed + ". I am big.  " + "I am " + dogHeight + " cm high! " +
             "My coat is " + dogColour + barkSound;
             return dogSpeech;

         }
         else
         {

             dogSpeech = "Hello. I am a " + breed + ". I am small. " + "I am " + dogHeight + " cm high! " +
             "My coat is " + dogColour + barkSound;
             return dogSpeech;
         }
     }

        public void SetSound(String barkSound)
        {
            this.barkSound = barkSound;
        } 
            public Dog(int dogHeight, string dogColour, string breed)
            {
                this.dogHeight = dogHeight;
                this.dogColour = dogColour;
                this.breed = breed;
            }
        }
    }

namespace HelloDogs
{
    class DogChorus
    {
        Dog lady;
        Dog tramp;
        Dog griff;
        Dog lass;

        public DogChorus()
        {
            lady = new Dog();
            tramp = new Dog();
            griff = new Dog();
            lass = new Dog();
            tramp.SetSound("Ruff!");
            lass.SetSound("Howl!");
        }

        public string GetOutput()
        {
            return Dog.GetSpeech() + " \n " + Dog.NoOfLegs() + " \n " + Dog.Getbreed();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您目前有这一行:

return Dog.GetSpeech() + " \n " + Dog.NoOfLegs() + " \n " + Dog.Getbreed();

虽然NoOfLegs是静态属性,但GetSpeechBreed不是。 (Getbreed甚至没有在任何地方声明)。这两个应该通过实例访问,例如lady.Breed以获取特定狗的数据。

因此,您需要访问每个实例GetSpeechBreed来电。也就是说:ladytrampgrifflass

此外,由于NoOfLegsBreed属性,而不是方法,因此不要将括号放在最后。只需Dog.NoOfLegslady.Breed

此外,GetSpeech()方法需要重写一些。它可能不应该采取任何参数。

祝你好运。