0

我想创建一个类对象的实例,以供多个私有函数以相同的形式使用。我目前拥有的是我创建一个新对象的每个函数,如下所示:

 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; }
}

}
4

2 回答 2

2

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

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

于 2013-04-19T23:18:46.743 回答
1

创建privatePlayer 的一个字段class并在您的各种方法中使用它。

例子:

public class TheClassThatUseThePlayerObject
{
    private 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));

    public void FirstMethod()
    {
        thePlayer.DoSomething();
    }
    public void SecondMethod()
    {
        thePlayer.DoSomethingElse();
    }
}

阅读有关字段的更多信息:字段

于 2013-04-19T23:23:14.250 回答