4

我正在尝试学习 C#,并且我正在尝试使用布尔值的示例。对于我的生活,我无法弄清楚为什么程序没有注意到我正在尝试将 true 值传递给布尔值。这是 Form.cs 中的代码:

namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        HappyBirthday birthdayMessage = new HappyBirthday();
        string returnedMessage;

        birthdayMessage.PresentCount = 5;
        birthdayMessage.MyProperty = "Adam";
        birthdayMessage.hasParty = true;
        returnedMessage = birthdayMessage.MyProperty;

        MessageBox.Show(returnedMessage);

    }
}
}

这是我创建的类:

class HappyBirthday
{

//====================
//  CLASS VARIABLES
//====================
private int numberOfPresents;
private string birthdayMessage;
private bool birthdayParty;

//===========================
//  DEFAULT CONSTRUCTOR
//===========================
public HappyBirthday()
{
    numberOfPresents = 0;
    //birthdayParty = false;
}

//===========================
//      METHOD
//===========================
private string getMessage(string givenName)
{

    string theMessage;

    theMessage = "Happy Birthday " + givenName + "\n";
    theMessage += "Number of presents = ";
    theMessage += numberOfPresents.ToString() + "\n";

    if (birthdayParty == true)
    {
        theMessage += "Hope you enjoy the party!";
    }
    else
    {
        theMessage += "No party = sorry!";
    }

    return theMessage;
}

//================================
//      READ AND WRITE PROPERTY
//================================
public string MyProperty
{
    get { return birthdayMessage; }

    set { birthdayMessage = getMessage(value); }
}

//================================
//     WRITE-ONLY PROPERTY
//================================
public int PresentCount
{
    set { numberOfPresents = value; }
}

public bool hasParty
{
    set { birthdayParty = value; }
}

}

现在我将初始值设置为false(即使我的理解是正确的应该是默认值),但是当我尝试设置它=true时,程序无法识别它。我应该以不同的方式传递布尔值而不是字符串或整数吗?

4

2 回答 2

7

你在设置MyProperty之前设置hasPartygetMessage()每次MyProperty轮询时都不会调用。

于 2013-10-18T20:22:42.587 回答
0

工作方式MyProperty令人困惑,因为setandget处理不同的值(你set的名字,然后get是整个消息,这令人困惑)。我会用一个GivenName属性替换它,然后制作GetMessage()(或将其公开为只读属性Messagepublic

此外,您可以通过使用自动属性使您的代码更简单(您可以使用private gets 来保持只写行为,尽管在现实世界中只写属性非常罕见,您可能应该像sets)。由于默认int值为0,因此您无需指定默认构造函数。下面是代码现在的样子:

class HappyBirthday
{
    public string Message
    {
        get
        {
            string theMessage;

            theMessage = "Happy Birthday " + GivenName + "\n";
            theMessage += "Number of presents = ";
            theMessage += PresentCount.ToString() + "\n";

            if (HasParty)
            {
                theMessage += "Hope you enjoy the party!";
            }
            else
            {
                theMessage += "No party = sorry!";
            }

            return theMessage;
        }
    }

    public string GivenName { private get; set; }

    public int PresentCount { private get; set; }

    public bool HasParty { private get; set; }
}
于 2013-10-18T20:29:40.843 回答