帮助错误C#:非静态字段,方法或属性RpgTutorial.Character.Swordsmanship需要对象引用

我对C#和编程很新,我在运行此代码时遇到错误(在标题框中描述):
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace RpgTutorial
{
    public class HeroSkills : Character
    {
        public int Skill()    
        {
            if (Hero.Swordsmanship = 10)
            {

            }
        }
    }
}
现在我知道我需要创建一个剑术的参考,但我究竟会怎么做呢?感谢您的任何帮助!     
已邀请:
如果您尝试访问该方法将要调用的同一对象的
Swordsmanship
属性,则可以通过
this
引用访问它:
if (this.Swordsmanship == 10) 
{
  ...
}
    
Hero
Character
的子类(或者相反)?如果是这样,你可以像这样引用属性
Swordsmanship
if (this.Swordsmanship == 10) 
{
   ...
}
否则,如果你发现自己需要引用“英雄”,你可以在你的
HeroSkills
类中添加一个构造函数(和属性),如下所示:
public HeroSkills : Character
{
   public Hero CurrentHero 
   {
      get;
      set;
   }

   public HeroSkills(Hero hero)
   {
      this.CurrentHero = hero;
   }
   ...
请注意,
this
关键字不是必需的,但表示您正在访问的属性是您的类的成员。这可以在以后帮助您提高可读性。然后你可以用各种方法引用你的班级
CurrentHero
,如
Skill()
,如下:
if (this.CurrentHero.Swordsmanship == 10) 
{
   ...
}
您可以在代码中的其他地方使用新修改的类,如下所示:
Hero player1 = //some hero variable
var skills = new HeroSkills(player1);
int currentSkill = skills.Skill();
    

要回复问题请先登录注册