在switch语句

时间:2016-11-07 22:59:11

标签: c# visual-c++

这是我第一次编写如此复杂的代码,我偶然发现了一个问题。 所以,我正在尝试使用visual studio在visual C#中创建一个非常基本的基于文​​本的RPG。 到目前为止,我有3个表格。主菜单表单(带有播放和退出按钮),字符选择表单(单击播放后打开)和游戏表单本身。 在角色选择表单上,提示用户选择其角色类(法师,战士,流氓)。 我创建了一个名为entity的公共抽象类:

    public abstract class Entity
{
    #region Attributes

    protected string _name;
    protected int _strength, _strengthModifier,
        _magic, _magicModifier,
        _luck, _luckModifier,
        _defense, _defenseModifier,
        _health, _healthModifier;

    #endregion

    #region Properties

    public string Name
    {
        get { return _name; }
        protected set { _name = value; }
    }
    public int Strength
    {
        get { return _strength; }
        protected set { _strength = value; }
    }
    public int StrengthModifier
    {
        get { return _strengthModifier; }
        protected set { _strengthModifier = value; }
    }
    public int Magic
    {
        get { return _magic; }
        protected set { _magic = value; }
    }
    public int MagicModifier
    {
        get { return _magicModifier; }
        protected set { _magicModifier = value; }
    }
    public int Luck
    {
        get { return _luck; }
        protected set { _luck = value; }
    }
    public int LuckModifier
    {
        get { return _luckModifier; }
        protected set { _luckModifier = value; }
    }
    public int Defense
    {
        get { return _defense; }
        protected set { _defense = value; }
    }
    public int DefenseModifier
    {
        get { return _defenseModifier; }
        protected set { _defenseModifier = value; }
    }
    public int Health
    {
        get { return _health; }
        protected set { _health = value; }
    }
    public int HealthModifier
    {
        get { return _healthModifier; }
        protected set { _healthModifier = value; }
    }

    #endregion

    #region Constructor
    public Entity()
    {
        Name = "";
        Strength = 0;
        Magic = 0;
        Luck = 0;
        Defense = 0;
        Health = 0;
    }
    #endregion

    #region Methods

    #endregion

}

然后我用这个继承创建了warrior类,age类和rogue类。

class Mage : Entity
{
    public Mage(string name) : base()
    {
        _name = name;

            #region Stats
        _strength = 0;
        _strengthModifier = 0;
        _magic = 10;
        _magicModifier = 3;
        _luck = 3;
        _luckModifier = 0;
        _defense = 5;
        _defenseModifier = 0;
        _health = 100;
        _healthModifier = 0;
            #endregion
    }
}

现在,我在游戏表单上创建了类对象。从角色选择表单中我调用游戏表单,然后将玩家姓名和类别作为参数发送。

        public Game(string name, string characterClass)
    {
        InitializeComponent();

        #region CreateCharacter

        switch (characterClass)
        {
            case "Mage":
                {
                    Mage player = new Mage(name); // create player

                    #region PrintPicture

                    pbPlayer.Image = Image.FromFile("C:/Users/mircea/Documents/Visual Studio 2015/Projects/RPG/Artwork/Characters/Mage.png");
                    pbPlayer.BackgroundImageLayout = ImageLayout.Center;
                    pbPlayer.Size = new Size(145, 200);

                    #endregion

                    lblName.Text = player.Name.ToString(); // show player name
                    txtLog.Text += "Welcome to game title, " + name + "!"; // greet log
                    break;
                }
            case "Rogue":
                {
                    Rogue player = new Rogue(name); // create player

                    #region PrintPicture

                    pbPlayer.Image = Image.FromFile("C:/Users/mircea/Documents/Visual Studio 2015/Projects/RPG/Artwork/Characters/Rogue.bmp");
                    pbPlayer.BackgroundImageLayout = ImageLayout.Center;
                    pbPlayer.Size = new Size(145, 200);

                    #endregion

                    lblName.Text = player.Name.ToString(); // show player name
                    txtLog.Text += "Welcome to game title, " + name + "!"; // greet log
                    break;
                }
            case "Warrior":
                {
                    Warrior player = new Warrior(name); // create player

                    #region PrintPicture

                    pbPlayer.Image = Image.FromFile("C:/Users/mircea/Documents/Visual Studio 2015/Projects/RPG/Artwork/Characters/Warrior.png");
                    pbPlayer.BackgroundImageLayout = ImageLayout.Center;
                    pbPlayer.Size = new Size(145, 200);

                    #endregion

                    lblName.Text = player.Name.ToString(); //show player name
                    txtLog.Text += "Welcome to game title, " + name + "!"; // greet log
                    break;
                }
        } 

        #endregion


    }

现在。如果我尝试在该开关之外调用播放器,它将无法正常工作。我能看到的唯一解决方案是将代码分配3次,每种情况一次。无论如何,我是1000%肯定不是这样的。 有什么建议吗?

1 个答案:

答案 0 :(得分:0)

问题是因为您在本地画笔中实例化对象,也就是说,您将无法从交换机外部访问它们。如果要在交换机外部使用它们,则需要在交换机外部声明它们。像这样:

public Game(string name, string characterClass)
    {
        InitializeComponent();

        #region CreateCharacter

        Entity player = null;
        switch (characterClass)
        {
            case "Mage":
                {
                    player = new Mage(name); // create player

                    #region PrintPicture

                    pbPlayer.Image = Image.FromFile("C:/Users/mircea/Documents/Visual Studio 2015/Projects/RPG/Artwork/Characters/Mage.png");
                    pbPlayer.BackgroundImageLayout = ImageLayout.Center;
                    pbPlayer.Size = new Size(145, 200);

                    #endregion

                    lblName.Text = player.Name.ToString(); // show player name
                    txtLog.Text += "Welcome to game title, " + name + "!"; // greet log
                    break;
                }
            case "Rogue":
                {
                    player = new Rogue(name); // create player

                    #region PrintPicture

                    pbPlayer.Image = Image.FromFile("C:/Users/mircea/Documents/Visual Studio 2015/Projects/RPG/Artwork/Characters/Rogue.bmp");
                    pbPlayer.BackgroundImageLayout = ImageLayout.Center;
                    pbPlayer.Size = new Size(145, 200);

                    #endregion

                    lblName.Text = player.Name.ToString(); // show player name
                    txtLog.Text += "Welcome to game title, " + name + "!"; // greet log
                    break;
                }
            case "Warrior":
                {
                    player = new Warrior(name); // create player

                    #region PrintPicture

                    pbPlayer.Image = Image.FromFile("C:/Users/mircea/Documents/Visual Studio 2015/Projects/RPG/Artwork/Characters/Warrior.png");
                    pbPlayer.BackgroundImageLayout = ImageLayout.Center;
                    pbPlayer.Size = new Size(145, 200);

                    #endregion

                    lblName.Text = player.Name.ToString(); //show player name
                    txtLog.Text += "Welcome to game title, " + name + "!"; // greet log
                    break;
                }
        } 

        #endregion


    }

我只是将播放器的声明放在带抽象类型的开关上。

通过在切换之前声明对象,您将能够访问它。由于所有对象都从实体继承,而实体是一个抽象类,因此您可以使用它来键入对象。

这是我看到解决问题的最好方法。