在switch语句中声明类对象

本文关键字:对象 声明 switch 语句 | 更新日期: 2023-09-27 18:17:02

这是我第一次编写如此复杂的代码,我偶然发现了一个问题。所以,我正在尝试制作一个非常基本的基于文本的RPG,在visual c#使用visual studio。到目前为止,我有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
}

然后我创建了战士职业,年龄职业和盗贼职业,继承了这个。

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

    }

。如果我尝试在这个开关之外调用player,它将不起作用。我能想到的唯一解决方案是代码三次,每次一次。不管怎么说,我百分之百肯定不是这样的。有什么建议吗?

在switch语句中声明类对象

问题是因为您在本地刷中实例化了对象,也就是说,您将无法从交换机外部访问它们。如果你想在switch之外使用它们,你需要在switch之外声明它们。这样的:

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

    }

我只是把玩家的陈述放在抽象类型的开关上。

通过在switch之前声明对象,您将能够在其内部和外部访问它。因为所有的对象都继承自entity,而entity是一个抽象类,所以你可以用它来对对象进行类型划分。

这是我认为解决你问题的最好办法。