4

我有一个基类和 2 个派生类。基类有一些简单的受保护浮点变量,我的基类的构造函数如下:

public Enemy(float _maxhp, float _damage)
{
    maxhp = _maxhp;
    health = _maxhp;
    damage = _damage;
}

但是,我的派生类Range还有 2 个浮点变量attack and attackSpeed需要在创建新实例时作为参数传入,Range但我似乎无法做到这一点,因为There is no argument that corresponds to the required formal parameter '_maxhp' of 'Enemy.Enemy(float, float)'当我尝试使用派生的构造函数时出现错误提示具有这些参数的类:

public Range(float _maxhp, float _damage, float _attack, float _attackSpeed)

但是具有相同数量参数的构造函数可以工作

public Range(float _maxhp, float _damage)

为什么会发生这种情况,是否有某种解决方法?提前致谢。

4

2 回答 2

14

您必须指定如何使用base()构造函数调用在基类上调用构造函数:

public Range(float _maxhp, float _damage, float _attack, float _attackSpeed)
    : base(_maxhp, _damage)
{
    // handle _attack and _attackSpeed
}
于 2018-08-07T12:05:32.117 回答
3

试试这个-

public Range(float _maxhp, float _damage, float _attack, float _attackSpeed) : base(_maxhp, _damage)
{
this.attack = _attack;
this.attackSpeed = _attackSpeed;
}
于 2018-08-07T12:06:52.150 回答