从构造函数继承

时间:2015-09-24 01:14:36

标签: c# inheritance constructor polymorphism

我需要帮助了解如何让某些事情发挥作用。

我已经获得了我的基类Hero(),我从中导出了其他实例,即Fighter()Archer()。我试图在Hero()类中专门设置很多逻辑,所以我减少了所需的重新输入量。以下是我的课程:

class Hero
{
    public String Name { get; set; }

    public int MaxHP { get; set; }

    public int BaseHP { get; set; }

    public int CurrentHP { get; set; }

    public int BaseDodge { get; set; }

    public int Dodge { get; set; }

    public int BaseAttack { get; set; }

    public int Attack { get; set; }

    public int Damage { get; set; }

    public int BaseXP { get; set; }

    public int CurrentXP { get; set; }

    public int NeededXP { get; set; }

    public int Level { get; set; }

    public int HPLevelInc { get; set; }

    public int AttackLevelInc { get; set; }

    public int DodgeLevelInc { get; set; }

    public int DodgeCap { get; set; }

    public string Ability1Name { get; set; }

    public string Ability2Name { get; set; }

    public string Ability3Name { get; set; }

    public int Ability1Mod { get; set; }

    public int Ability2Mod { get; set; }

    public int Ability3Mod { get; set; }

    public int Ability2CoolDown { get; set; }

    public int Ability3CoolDown { get; set; }

    public int BaseHealMod { get; set; }

    public int HealMod { get; set; }

    public int HealCap { get; set; }

    public int HealLevelInc { get; set; }

    public Hero(int lvl)
    {
        BaseXP = 1000;
        NeededXP = 1000;
        /*
        MaxHP = BaseHP;
        Dodge = BaseDodge;
        Attack = BaseAttack;
        HealMod = BaseHealMod;
        */
        if (lvl > 1)
        {
            for (int i = 0; i < lvl; i++)
            {
                LevelUp();
            }
        }
    }
    public int getDamage(int abilMod)
    {
        Random r = new Random();
        int randDmg = r.Next(1, Level);
        Damage = Attack + abilMod + randDmg;
        return Damage;
    }
    public void LevelUp()
    {
        BaseXP = (int)Math.Floor(BaseXP * 2.5);
        NeededXP = BaseXP - CurrentXP;
        Level++;
        MaxHP = MaxHP + HPLevelInc;
        CurrentHP = MaxHP;
        Attack = Attack + AttackLevelInc;
        if (Level % 5 == 0 && Dodge < DodgeCap)
            Dodge = Dodge + DodgeLevelInc;
        if (Level % 5 == 0 && HealMod < HealCap)
            HealMod = HealMod + HealLevelInc;
    }
    public void Heal()
    {
        CurrentHP += HealMod;
        if (CurrentHP > BaseHP)
        {
            CurrentHP = BaseHP;
        }
    }
    public void Heal(int extra)
    {
        CurrentHP += HealMod + extra;
        if (CurrentHP > BaseHP)
        {
            CurrentHP = BaseHP;
        }
    }
}

然后我的Figher()看起来像这样:

class Fighter : Hero
{
    public Fighter(int lvl) : base(lvl)
    {
        Name = "Fighter";
        BaseAttack = 10;
        BaseHP = 50;
        BaseDodge = 5;
        HPLevelInc = 7;
        AttackLevelInc = 3;
        DodgeLevelInc = 1;
        DodgeCap = 45;
        BaseHealMod = 5;
        HealCap = 30;
        HealLevelInc = 3;
        Ability1Name = "Ability 1";
        Ability2Name = "Ability 2";
        Ability3Name = "Ability 3";
        Ability2CoolDown = 3;
        Ability3CoolDown = 5;
    }
}

我遇到的问题是,当我创建Fighter()的实例时,它会增加级别和XP,但没有别的。

class Program
{
    static void Main(string[] args)
    {
        Fighter F = new Fighter(7);

        Console.WriteLine("Level: " + F.Level + " " + F.Name);
        Console.WriteLine("Hit Points: " + F.CurrentHP);
        Console.WriteLine("cXP: " + F.CurrentXP + "  XP2L: " + F.NeededXP);

        Console.ReadKey();
    }
}

输出为:

Level: 7 Fighter
Hit Points: 0
cXP: 0  XP2L: 610342

有人可以帮助我了解如何实现这一点并让它发挥作用吗?我想我错过了一些简单的东西,但我不确定是什么。

2 个答案:

答案 0 :(得分:0)

你有明显的输出,因为基础构造函数在派生的&amp;在这一点上,这些值不是用你在Fighter的构造函数中设置的值初始化的。

使用实例变量初始化器有一种hacky方式:

using System;

class Hero
{
    public Hero()
    {
        //code
    }  
}

class Fighter: Hero
{
    private readonly int ignoreMe = BeforeBaseConstructorCall();

    public Fighter()
    {
    }

    private static int BeforeBaseConstructorCall()
    {
        //initailise the properties.
    }
}

class Test
{
    static void Main()
    {
          //calls Fighter first & then Base()
          Fighter obj =  new Fighter();
    }    
}

答案 1 :(得分:0)

你的等级增加的原因是因为在实例化Fighter子类时它还会实例化你的基类Hero并在你的基础中运行逻辑。

罪魁祸首是你设计基地的方式。 并且你不应该尽可能地将任何逻辑放在构造函数中导致你的问题。

你的基础Hero构造函数需要参数来强制你的子类在子构造函数中实例化基础Hero,并且所有将从你的基类继承的东西都将被强制执行。

这就是为什么你有这个:

public class Hero(int lvl) { ... }

public Fighter(int lvl) : base(lvl) {...}

如果要将级别传递给基础Hero类而不在子类中实例化基础Hero,请删除基础构造函数参数并将其调整为非参数化并将逻辑放在其他位置。从base继承的子类可以使用它的任何属性。

这样的事情:

public Hero()
{
  public int level;
  ...
}

public class Fighter : Hero   
{ 
   public class Fighter(int lvl)
   {
      base.level = lvl;
   }
}