派生类的构造函数可以具有比其基类的构造函数更多的参数吗?

Ryo*_*olu 3 c# constructor class unity-game-engine

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

public Enemy(float _maxhp, float _damage)
{
    maxhp = _maxhp;
    health = _maxhp;
    damage = _damage;
}
Run Code Online (Sandbox Code Playgroud)

但是,我的派生类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)

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

public Range(float _maxhp, float _damage)

为什么会发生这种情况并且有某种解决方法?提前致谢.

Pat*_*man 11

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

public Range(float _maxhp, float _damage, float _attack, float _attackSpeed)
    : base(_maxhp, _damage)
{
    // handle _attack and _attackSpeed
}
Run Code Online (Sandbox Code Playgroud)