C#OOP打印结果

时间:2016-10-23 09:08:26

标签: c# visual-studio oop

我是视觉工作室中的c#的新手,我试图使用OOP来打印结果  我想将结果打印为:

  

您好。我是一只可卡犬。纬!我是(大或小),颜色是   白色,我有4条腿。

由于我设定的值是10所以它总是很小......

这是我的代码:

public Dog()
        {
            barkSound = "Woof!";
            breed = "cocker spaniel";
            dogHeight = 10;
            dogColour = "white";
            noOfLEgs = 4;
        }

  public string GetSpeech()
    {
        dogSpeech = "Hello. I am a " + breed + ". " + barkSound + " I'm " 
                    + dogHeight + ", Colour is " + dogColour + ", I have " + noOfLEgs +" legs" ;
        return dogSpeech;
    }

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

    }

我应该修改&#34; IsBig&#34;方法或其他更好的方式打印结果如期望? 希望我能坚持使用布尔值。 谢谢

4 个答案:

答案 0 :(得分:1)

您可以修改方法

private string GetSizeString(int dogHeight)
{
    if (dogHeight < 50)
    {
        return "Small";
    }
    return "Big";
}

然后简单地

public string GetSpeech()
{
    return "Hello. I am a " + breed + ". " + barkSound + " I'm " 
                + GetSizeString(dogHeight) + ", Colour is " + dogColour + ", I have " + noOfLEgs +" legs" ;
}

答案 1 :(得分:1)

您不应该修改IsBig方法。也许你应该把这个作为财产,但这不是太重要。你可以制作一个新方法:

private string GetSizeText(bool isBig)
{
   if (isBig)
     return "Big";

   return "Small";
}

然后你的GetSpeech看起来像:

  public string GetSpeech()
    {
        dogSpeech = "Hello. I am a " + breed + ". " + barkSound + " I'm " 
                    + GetSizeText(IsBig(dogHeight)) + ", Colour is " + dogColour + ", I have " + noOfLEgs +" legs" ;
        return dogSpeech;
    }

这是一个简单的解决方案。您还可以创建一个包含Big和Small值的枚举,并使用ToString();打印此枚举值。

答案 2 :(得分:1)

有两种方法可以做到这一点。

您可以将IsBig重命名为GetSizeDescription并返回一个字符串:

private string GetSizeDescription() {
    if (dogHeight < 50)
    {
        return "Small";
    } else
    {
        return "Big";
    }
}

用法:

public string GetSpeech()
{
    dogSpeech = "Hello. I am a " + breed + ". " + barkSound + " I'm " 
                + GetSizeDescription() + ", Colour is " + dogColour + ", I have " + noOfLEgs +" legs" ;
    return dogSpeech;
}

或者,您可以保持IsBig方法不变,并将GetSpeech方法更改为:

public string GetSpeech()
{
    dogSpeech = "Hello. I am a " + breed + ". " + barkSound + " I'm " 
                + (IsBig() ? "Big" : "Small") + ", Colour is " + dogColour + ", I have " + noOfLEgs +" legs" ;
    return dogSpeech;
}

答案 3 :(得分:1)

这是您的完整代码。

public class Dog
{

    public Dog()
    {
        barkSound = "Woof!";
        breed = "cocker spaniel";
        dogHeight = 10;
        dogColour = "white";
        noOfLEgs = 4;
    }

    public string barkSound { get; private set; }
    public string breed { get; private set; }
    public string dogColour { get; private set; }
    public int dogHeight { get; private set; }
    public int noOfLEgs { get; private set; }

    public string GetSpeech()
    {

        string dogSpeech = "Hello. I am a " + breed + ". " + barkSound + " I'm "
                    + (IsBig(dogHeight) ? "Big" : "Small") + ", Colour is " + dogColour + ", I have " + noOfLEgs + " legs";
        return dogSpeech;
    }

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

    }
}

通话应该是这样的。

Dog dog = new Dog();
string speech = dog.GetSpeech();

dogHeight替换为(IsBig(dogHeight) ? "Big" : "Small")即可。