在Unity中继承构造函数?

huangapple go评论67阅读模式
英文:

Inheriting Constructors in Unity?

问题

为了提供上下文,我从这个教程 中获取了这段代码,特别是在2:27:28处。

我有两个类,PlayerWarrior。我想让 Warrior 类继承自 Player 类。Player 类如下所示:

public class Player : MonoBehaviour
{
    // 变量和属性
    private string _playerName;
    public string pName { get => _playerName; set => _playerName = value; }

    private int _playerHealth;
    public int Health { get => _playerHealth; set => _playerHealth = value; }

    private int _playerPower;
    public int Power { get => _playerPower; set => _playerPower = value; }

    // 构造函数
    public Player() { } // 构造函数 1

    public Player(string name, int health, int power) // 构造函数 2
    {
        pName = name;
        Health = health;
        Power = power;
    }
}

Warrior 类如下所示:

public class Warrior : Player
{
    public Warrior(string name, int health, int power)
    {
        pName = name;
        Health = health;
        Power = power;
    }
}

每当我移除 Player 类中的构造函数 1(即空白的构造函数),我在 Warrior 构造函数中会收到一个错误。但如果我移除 Warrior 构造函数本身,那么在声明 Warrior 类本身时会出现错误。我不明白为什么需要添加构造函数 1,尤其是当我已经有 Player 类中的构造函数 2 时。

我是否遗漏了什么?或者父类是否总是需要额外的构造函数,每当子类被声明时?如果是这样,为什么?或者在UNITY中继承构造函数工作方式不同吗?

我尝试移除构造函数 1 并在VS中使用 CTRL + . 命令,甚至调试器也让我感到困惑。我还尝试在网上搜索,但没有找到答案。

英文:

For context, I got this code from this tutorial, particularly at 2:27:28.

I have two classes, Player and Warrior. I want the Warrior class to inherit from the Player class. The Player class looks like this:

public class Player: MonoBehaviour
{
    //Variables and Properties
    private string _playerName;
    public string pName { get => _playerName; set => _playerName = value; }

    private int _playerHealth;
    public int Health { get => _playerHealth ; set => _playerHealth = value ; }

    private int _playerPower;
    public int Power { get => _playerPower; set => _playerPower = value ; }


    //Constructors
    public Player() { } //Constructor 1

    public Player(string name, int health, int power) //Constructor 2
    {
        pName = name;
        Health = health;
        Power = power;
    }
}

And the Warrior class looks like this:

public class Warrior : Player
{
    public Warrior(string name, int health, int power)
    {
        pName = name;
        Health = health;
        Power = power;
    }
}

Whenever I remove Constructor 1 in Player (the blank one), I get an error in the Warrior constructor. But if I remove the Warrior Constructor itself, then I get an error when declaring the Warrior class itself. I don't understand why it would be necessary to add Constructor 1, especially when I already have Constructor 2 in the Player class, to begin with.

Is there something I am missing? Or does a parent class always need an extra constructor whenever the child is being declared? And if so, Why? Or maybe inheriting constructors in UNITY works differently?

I tried removing Constructor 1 and using the command CTRL + . in VS, and even the debugger confuses me. I also tried googling online to no avail.

答案1

得分: -1

像rshepp说的,你可能不应该在MonoBehaviors中使用构造函数。而是使用Start()和Awake()来初始化。

英文:

Like rshepp said, you probably shouldn't use constructors with MonoBehaviors. Instead, use Start() and Awake() to initialize.

huangapple
  • 本文由 发表于 2023年6月29日 09:00:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定