如何在程序中调用我的私有void方法

时间:2015-12-01 06:32:21

标签: c#

因此,对于这项任务,我必须使用3种方法并让它显示正确的东西,但我似乎无法弄清楚如何调用我所做的方法。我查看了我的书并在网上搜索,找不到任何东西。希望有人可以帮助我!

            InitializeComponent();
    }
    const int ROCK = 1;
    const int PAPER = 2;
    const int SCISSORS = 3;
    int userWins = 0;
    int computerWins = 0;


    private void RockButton(int userChoice, int computerChoice, int PAPER, int SCISSORS)
    {
        if (userChoice == computerChoice)
            MessageBox.Show("It's a tie!");
        else if (computerChoice == PAPER)
        {
            MessageBox.Show("You lose! Paper covers rock.");
        }
        else if (computerChoice == SCISSORS)
        {
            MessageBox.Show("You win! Rock crushes scissors.");
        }
    }

    private void PaperButton(int userChoice, int computerChoice, int ROCK, int SCISSOR)
    {
        if (userChoice == computerChoice)
            MessageBox.Show("It's a tie!");
        else if (computerChoice == ROCK)
        {
            MessageBox.Show("You win! Paper covers rock.");
        }
        else if (computerChoice == SCISSORS)
        {
            MessageBox.Show("You lose! Scissors cuts paper.");
        }
    }

    private void ScissorsButton(int userChoice, int computerChoice, int PAPER, int ROCK)
    {
        if (userChoice == computerChoice)
            MessageBox.Show("It's a tie!");
        else if (computerChoice == PAPER)
        {
            MessageBox.Show("You win! Scissors cuts paper");
        }
        else if (computerChoice == ROCK)
        {
            MessageBox.Show("You lose! Rock crushes scissors.");
        }
    }


    private void rockButton_Click(object sender, EventArgs e)
    {

        userPictureBox.Image = Properties.Resources.Rock;

        Random randomNumberGenerator = new Random();
        int computerChoice = randomNumberGenerator.Next(1, 4);
        switch (computerChoice)
        {
            case ROCK:
                computerPictureBox.Image = Properties.Resources.Rock;
                break;

            case PAPER:
                computerPictureBox.Image = Properties.Resources.Paper;
                break;

            case SCISSORS:
                computerPictureBox.Image = Properties.Resources.Scissors;
                break;
        }
    }

    private void paperButton_Click(object sender, EventArgs e)
    {

        userPictureBox.Image = Properties.Resources.Paper;

        Random randomNumberGenerator = new Random();
        int computerChoice = randomNumberGenerator.Next(1, 4);
        switch (computerChoice)
        {
            case ROCK:
                computerPictureBox.Image = Properties.Resources.Rock;
                break;

            case PAPER:
                computerPictureBox.Image = Properties.Resources.Paper;
                break;

            case SCISSORS:
                computerPictureBox.Image = Properties.Resources.Scissors;
                break;

        }

        userWinsLabel.Text = userWins.ToString();
        computerWinsLabel.Text = computerWins.ToString();
    }

    private void scissorsButton_Click(object sender, EventArgs e)
    {

        userPictureBox.Image = Properties.Resources.Scissors;

        Random randomNumberGenerator = new Random();
        int computerChoice = randomNumberGenerator.Next(1, 4);
        switch (computerChoice)
        {
            case ROCK:
                computerPictureBox.Image = Properties.Resources.Rock;
                break;

            case PAPER:
                computerPictureBox.Image = Properties.Resources.Paper;
                break;

            case SCISSORS:
                computerPictureBox.Image = Properties.Resources.Scissors;
                break;

        }
    }

    private void playAgainButton_Click(object sender, EventArgs e)
    {
        Application.Restart();
    }
}}

如何在单击按钮时调用这些方法,它将显示方法所声明的内容?

1 个答案:

答案 0 :(得分:1)

您无法从包含类范围之外调用private成员(反射放在一边)。

对于可从其他类/对象调用的成员,这些成员必须至少为internal(当调用者驻留在同一程序集中时)或public

修改

如果调用类继承自包含成员的类,则protected修饰符也可以。