为几个私有函数创建一个类对象的实例

时间:2013-04-19 23:15:59

标签: c# class object

我想创建一个类对象的实例,以便由同一表单中的多个私有函数使用。我目前拥有的是我创建一个新对象的每个函数:

 Player thePlayer = new Player(int.Parse(StrBox.Text), int.Parse(DexBox.Text), int.Parse(IntBox.Text), int.Parse(PerBox.Text), int.Parse(HPBox.Text), int.Parse(SPBox.Text), int.Parse(MPBox.Text), int.Parse(EXPBox.Text), int.Parse(ARBox.Text), int.Parse(CTHBox.Text), int.Parse(GoldBox.Text), int.Parse(MeleeDMGBox.Text), int.Parse(MagicDMGBox.Text), int.Parse(StealthBox.Text), int.Parse(DetectBox.Text), int.Parse(LevelBox.Text));

我必须把它放到四个不同的功能中。你可以看到它是一个包含许多变量的巨大对象。我希望能够创建一次以供所有对象使用,因为当我创建一个新变量时,我最终不得不在我的函数中复制粘贴这个野兽。

我该怎么做?我是OOP的新手,所以这就是我试图做的,不知道它是否会起作用:

    public void PlayerInitialize()
    {
        string P = "p";
        Player thePlayer = new Player(int.Parse(StrBox.Text), int.Parse(DexBox.Text),      int.Parse(IntBox.Text), int.Parse(PerBox.Text), int.Parse(HPBox.Text), int.Parse(SPBox.Text), int.Parse(MPBox.Text), int.Parse(EXPBox.Text), int.Parse(ARBox.Text), int.Parse(CTHBox.Text), int.Parse(GoldBox.Text), int.Parse(MeleeDMGBox.Text), int.Parse(MagicDMGBox.Text), int.Parse(StealthBox.Text), int.Parse(DetectBox.Text), int.Parse(LevelBox.Text), P);
    }
  private void AddStrButton_Click_1(object sender, EventArgs e)
    {
 Player thePlayer;
        //int Str, int Dex, int Intel, int Per, int HP, int SP, int MP, int EXP, int AR, int CTH, int Gold, int MelDMG, int MagDMG, int Stlth, int Det
        //Player thePlayer = new Player(int.Parse(StrBox.Text), int.Parse(DexBox.Text), int.Parse(IntBox.Text), int.Parse(PerBox.Text), int.Parse(HPBox.Text), int.Parse(SPBox.Text), int.Parse(MPBox.Text), int.Parse(EXPBox.Text), int.Parse(ARBox.Text), int.Parse(CTHBox.Text), int.Parse(GoldBox.Text), int.Parse(MeleeDMGBox.Text), int.Parse(MagicDMGBox.Text), int.Parse(StealthBox.Text), int.Parse(DetectBox.Text), int.Parse(LevelBox.Text), );


        if (thePlayer.StatPoints >= 1 && thePlayer.Strength <= 7)
        {
            thePlayer.Strength++;
            thePlayer.StatPoints--;
            thePlayer.HitPoints += 6;
            thePlayer.MeleeDMG += 2;
            StrBox.Text = thePlayer.Strength.ToString();
            SPBox.Text = thePlayer.StatPoints.ToString();
            HPBox.Text = thePlayer.HitPoints.ToString();
            MeleeDMGBox.Text = thePlayer.MeleeDMG.ToString();
        }
        else if (thePlayer.Strength >= 8)
        {
            MessageBox.Show("You are at the limits of your mortal abilities.");
        }
        else if (thePlayer.StatPoints <= 0)
        {
            MessageBox.Show("Earn more experience!");
        }
        /*
        MessageBox.Show("PCStats.StatPoints equals" + PCStats.StatPoints);
        MessageBox.Show("PCStats,Strength equals" + PCStats.Strength);
        MessageBox.Show("IntPCSP Equals" + IntPCSP.ToString());
        MessageBox.Show("IntPCStr Equals" + IntPCStr.ToString());
        */
    }

不幸的是我收到以下错误:使用未分配的局部变量'thePlayer'

关于如何创建对象而不是每次在函数中使用它的任何想法?

以下是课程表,供进一步参考:

 namespace PlayerPC
{
class Player
{
    public void PlayerGraphic(string pcGraphic)
    {
    PCGraphic = pcGraphic;
    }
    //player stats
    public Player(int Str, int Dex, int Intel, int Per, int HP, int SP, int MP, int EXP, int AR, int CTH, int Gold, int MelDMG, int MagDMG, int Stlth, int Det, int Lvl, string pcGraphic)
    {
        Strength = Str;
        Dexterity = Dex;
        Intelligence = Intel;
        Perception = Per;
        HitPoints = HP;
        StatPoints = SP;
        MindPoints = MP;
        ExperiencePoints = EXP;
        Armor = AR;
        ChanceToHit = CTH;
        GoldCoins = Gold;
        MeleeDMG = MelDMG;
        MagicDMG = MagDMG;
        Stealth = Stlth;
        Detect = Det;
        Level = Lvl;
        PCGraphic = pcGraphic;
    }
    public bool LevelUp()
    {
        if(this.Level == 1 && this.ExperiencePoints >= 25)
        {
            this.Level++;
            this.StatPoints++;
            MessageBox.Show("You have accended to Level " + this.Level);
            return true;
        }
        else if (this.Level == 2 && this.ExperiencePoints >= 125)
        {
            this.Level++;
            this.StatPoints++;
            MessageBox.Show("You have accended to Level " + this.Level);
            return true;
        }
        else if (this.Level == 3 && this.ExperiencePoints >= 625)
        {
            this.Level++;
            this.StatPoints++;
            MessageBox.Show("You have accended to Level " + this.Level);
            return true;
        }
        else if (this.Level == 4 && this.ExperiencePoints >= 1125)
        {
            this.Level++;
            this.StatPoints++;
            MessageBox.Show("You have accended to Level " + this.Level);
            return true;
        }
        else if (this.Level == 5 && this.ExperiencePoints >= 1850)
        {
            this.Level++;
            this.StatPoints++;
            MessageBox.Show("You have accended to Level " + this.Level);
            return true;
        }
        else if (this.Level == 6 && this.ExperiencePoints >= 2900)
        {
            this.Level++;
            this.StatPoints++;
            MessageBox.Show("You have accended to Level " + this.Level);
            return true;
        }
        else if (this.Level == 7 && this.ExperiencePoints >= 4500)
        {
            this.Level++;
            this.StatPoints++;
            MessageBox.Show("You have accended to Level " + this.Level);
            return true;
        }
        else if (this.Level == 8 && this.ExperiencePoints >= 5800)
        {
            this.Level++;
            this.StatPoints++;
            MessageBox.Show("You have accended to Level " + this.Level);
            return true;
        }
        else if (this.Level == 9 && this.ExperiencePoints >= 8200)
        {
            this.Level++;
            this.StatPoints++;
            MessageBox.Show("You have accended to Level " + this.Level);
            return true;
        }
        else if (this.Level == 10 && this.ExperiencePoints >= 12800)
        {
            this.Level++;
            this.StatPoints++;
            MessageBox.Show("You have accended to Level " + this.Level);
            return true;
        }
        else if (this.Level == 11 && this.ExperiencePoints >= 16200)
        {
            this.Level++;
            this.StatPoints++;
            MessageBox.Show("You have accended to Level " + this.Level);
            return true;
        }
        else if (this.Level == 12 && this.ExperiencePoints >= 25000)
        {
            this.Level++;
            this.StatPoints++;
            MessageBox.Show("You have accended to Level " + this.Level);
            return true;
        }

        return false;
    }

    public int Strength {get; set; }
    public int Dexterity { get; set; }
    public int Intelligence { get; set; }
    public int Perception { get; set; }
    public int HitPoints { get; set; }
    public int StatPoints { get; set; }
    public int MindPoints { get; set; }
    public int ExperiencePoints { get; set; }
    public int Armor { get; set; }
    public int ChanceToHit { get; set; }
    public int GoldCoins { get; set; }
    public int MeleeDMG { get; set; }
    public int MagicDMG { get; set; }
    public int Stealth { get; set; }
    public int Detect { get; set; }
    public int Level { get; set; }
    public string PCGraphic { get; set; }
}

}

2 个答案:

答案 0 :(得分:2)

将行Player thePlayer;放在方法定义之外,直接放入类中,并在其前面添加private

获得一本关于OOP的书并阅读基础知识。

答案 1 :(得分:1)

创建播放器private的{​​{1}}字段,并在各种方法中使用它。

示例:

class

详细了解字段:Fields