C#以另一种形式修改类的实例变量

时间:2017-12-05 09:13:17

标签: c#

对于我的一个项目,我正在构建视觉工作室形式的生活游戏。我目前面临的问题是我不知道在哪里初始化我的类对象,以便可以通过其他形式修改它们的值。

这是我的基本表格类,我在这里展示棋盘游戏和玩家的当前统计数据:

public partial class Life : Form
{
    public Game game;

    public Life()
    {
        game = new Game();
        InitializeComponent();
    }

    private void Life_Load(object sender, EventArgs e)
    {
        Txt_BlueMoney.Text = game.player1.money.ToString();
        Txt_RedMoney.Text = game.player2.money.ToString();
    }

    private void Btn_Start_Click(object sender, EventArgs e)
    {
        StartGame sg = new StartGame();
        sg.Show();


    }
}

我的Game类只是一个包含其他类实例的类:

public class Game
{
    public Board board;
    public BoardSpaceEvent boardEvent;
    public Player player1;
    public Player player2;

    public Game()
        : base()
    {
        board = new Board();
        boardEvent = new BoardSpaceEvent();
        player1 = new Player();
        player2 = new Player();
    }

}

以下是Game初始化的其中一个类的示例,以防其具有任何重要性:

public class Player : BoardSpaceEvent
{
    // might need to add getter and setter to access these variables
    public bool endOfTurn;
    public int currentSpace;
    public int numChildren;
    public int money;
    public int advanceSpaces;
    public List<int> playerLifeCards;
    public string career; // superstar, agent, teacher, athlete, salesperson, doctor, accountant, artist, police
    public int salary;
    public int taxes;
    public bool autoInsurance;
    public bool houseInsurance;

    public Player()
        : base()
    {
        endOfTurn = false; // might need to switch this
        currentSpace = 0;
        money = 10000; // will have to change this to whatever you start with
        numChildren = 0;
        advanceSpaces = 0; // make sure to pass this as reference
        playerLifeCards = new List<int> { };
        career = "";
        salary = 0;
        taxes = 0;
        autoInsurance = false;
        houseInsurance = false;
    }
}

现在我要做的是打开另一个用户必须在两个按钮之间选择的表单,并且每个按钮都将修改类实例变量。做这个的最好方式是什么?这是我到目前为止(我的评论显示了我想做的):

public partial class StartGame : Form
{
    public StartGame()
    {
        InitializeComponent();
    }

    private void Btn_College_Click(object sender, EventArgs e)
    {
        MessageBox.Show("You've chosen college");
        // this is where I want to update a players money variable in the Player class
        this.Close();
    }

    private void Btn_Job_Click(object sender, EventArgs e)
    {
        MessageBox.Show("You've chosen job");
    }
}

我尝试过传递作为参考但它没有用。也许我做错了。我也尝试从主窗体继承,但问题是背景也是继承的。我已经研究过使用静态实例,但这不是一种不好的做法吗?提前谢谢!

2 个答案:

答案 0 :(得分:0)

为类StartGame提供第二个构造函数,该构造函数可以将Game对象作为参数,使用Game localGame;类型的局部变量。

然后,当您打开public Game game;表单时,您可以从课程Life传递StartGame

private void Btn_Start_Click(object sender, EventArgs e)
{
    StartGame sg = new StartGame(game);
    sg.Show();
}

只需将StartGame构造函数中的传递参数分配给局部变量,您就可以在Btn_College_Click方法中使用它。

希望这会有所帮助。如果不清楚请给我发表评论

编辑:

  

我也尝试从主窗体继承,但问题是背景也是继承的。

即使继承是可能的,也无法解决您的问题。每个孩子都会拥有s own personal instance of Game`,这与父母拥有的对象不同。因此,更改childs对象不会影响父对象。

想象一下,您的孩子会继承您的头发颜色。现在,如果你把你孩子的头发染成不同的头发会发生什么?=!

答案 1 :(得分:0)

当我刚刚尝试时,这很有效:

public partial class Life : Form
{
    private int _money;

    public void AddMoney(int amount)
    {
        _money += amount;
        label1.Text = _money.ToString();
    }

    public Life()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
        var startGame = new StartGame(this);
        startGame.Show();
    }
}

public partial class StartGame : Form
{
    private readonly Life _life;

    public StartGame(Life life)
    {
        InitializeComponent();

        _life = life;
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
        _life.AddMoney(5);
    }
}