修改GetOutput()方法,使其返回c#的详细信息

本文关键字:返回 详细信息 GetOutput 方法 修改 | 更新日期: 2023-09-27 17:51:17

这是一种最后的手段。在我的一个实验室里,我遇到了一个问题,我无法解决它,甚至无法理解它,因为我真的不擅长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();
        }
    }
}

修改GetOutput()方法,使其返回c#的详细信息

你现在有这一行:

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

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

因此,您将希望访问每个实例GetSpeechBreed调用。即:ladytrampgrifflass

另外,由于NoOfLegsBreed属性,而不是方法,因此不需要在末尾加上括号。只有Dog.NoOfLegslady.Breed

同样,GetSpeech()方法需要一些重写。它可能不应该接受任何参数。

好运。