什么时候需要重写?我什么时候不需要它?

时间:2022-09-20 14:02:51

I am using C# with Unity3d.

我使用的是Unity3d的c#。

When I use Monobehaviour and create an Update method, I don't need to use the override keyword.

当我使用Monobehaviour并创建一个更新方法时,我不需要使用override关键字。

When I create my own class and override a function, I do need to use the override keyword.

当我创建自己的类并覆盖一个函数时,我确实需要使用override关键字。

Why is that? Why do I not need the override keyword when creating the Update method?

这是为什么呢?为什么在创建更新方法时不需要override关键字?

2 个解决方案

#1


3  

I think your confusion comes from the fact that Unity does something special about these methods (Update, Start, Awake, etc). You can declare them as private and even then they will be called. That's not achievable using the language unless you use reflection, but I was told they don't use it, so I don't know what they do. And honestly, it doesn't matter. Because you can think as this being an exception to the language, that is, these methods will be called if you implement them. Simply that.

我认为您的困惑来自于Unity对这些方法做了一些特别的事情(更新、启动、唤醒等等)。您可以将它们声明为私有的,即使这样,它们也会被调用。使用这种语言是不可能实现的,除非你使用反射,但我被告知他们不使用它,所以我不知道他们在做什么。老实说,这并不重要。因为您可以认为这是语言的一个异常,也就是说,如果您实现它们,这些方法将被调用。简单的。

For all the rest, you have to follow the language. Here is a rough explanation:

对于所有其他的,你必须遵循语言。这里有一个粗略的解释:

You can or have to override a method if it is marked as abstract or virtual in the base class.

如果一个方法在基类中被标记为抽象或虚,则可以或必须重写它。

A method is abstract when the base class wants its children to implement it. A method is virtual when the base class offers an implementation of it but also offers an opportunity for the children to implement/modify that method.

当基类希望子类实现方法时,方法就是抽象的。当基类提供它的实现时,方法是虚拟的,但同时也为孩子提供了实现/修改该方法的机会。

So why can't all methods be "overridable"? To protect the base class developer's intention. You'd be changing the behaviour of a base class, you don't know if the base class developer would like you to do this. It's like a security lock. So that's why you have the three words abstract, virtual and override, to communicate the API intentions from the base class to their children.

那么为什么所有的方法都不能“可重写”呢?保护基类开发人员的意图。您将改变基类的行为,您不知道基类开发人员是否希望您这样做。就像锁一样。这就是为什么要用抽象、虚拟和重写这三个词,将API意图从基类传递给它们的子类。

#2


1  

You need to use the override, when you derive a class from another class and you need to change the code of a method of the base class that is virtual. This way the inheritted method of the base class can have a different behaviour -more suitable- for the derived class.

当您从另一个类派生类时,需要使用覆盖,并且需要更改虚拟基类的方法的代码。这样,基类的继承方法就可以有不同的行为——更适合于派生类。

More generally, as it is stated in MSDN:

更一般地说,如MSDN所述:

The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

重写修饰符用于扩展或修改继承的方法、属性、索引器或事件的抽象或虚拟实现。

#1


3  

I think your confusion comes from the fact that Unity does something special about these methods (Update, Start, Awake, etc). You can declare them as private and even then they will be called. That's not achievable using the language unless you use reflection, but I was told they don't use it, so I don't know what they do. And honestly, it doesn't matter. Because you can think as this being an exception to the language, that is, these methods will be called if you implement them. Simply that.

我认为您的困惑来自于Unity对这些方法做了一些特别的事情(更新、启动、唤醒等等)。您可以将它们声明为私有的,即使这样,它们也会被调用。使用这种语言是不可能实现的,除非你使用反射,但我被告知他们不使用它,所以我不知道他们在做什么。老实说,这并不重要。因为您可以认为这是语言的一个异常,也就是说,如果您实现它们,这些方法将被调用。简单的。

For all the rest, you have to follow the language. Here is a rough explanation:

对于所有其他的,你必须遵循语言。这里有一个粗略的解释:

You can or have to override a method if it is marked as abstract or virtual in the base class.

如果一个方法在基类中被标记为抽象或虚,则可以或必须重写它。

A method is abstract when the base class wants its children to implement it. A method is virtual when the base class offers an implementation of it but also offers an opportunity for the children to implement/modify that method.

当基类希望子类实现方法时,方法就是抽象的。当基类提供它的实现时,方法是虚拟的,但同时也为孩子提供了实现/修改该方法的机会。

So why can't all methods be "overridable"? To protect the base class developer's intention. You'd be changing the behaviour of a base class, you don't know if the base class developer would like you to do this. It's like a security lock. So that's why you have the three words abstract, virtual and override, to communicate the API intentions from the base class to their children.

那么为什么所有的方法都不能“可重写”呢?保护基类开发人员的意图。您将改变基类的行为,您不知道基类开发人员是否希望您这样做。就像锁一样。这就是为什么要用抽象、虚拟和重写这三个词,将API意图从基类传递给它们的子类。

#2


1  

You need to use the override, when you derive a class from another class and you need to change the code of a method of the base class that is virtual. This way the inheritted method of the base class can have a different behaviour -more suitable- for the derived class.

当您从另一个类派生类时,需要使用覆盖,并且需要更改虚拟基类的方法的代码。这样,基类的继承方法就可以有不同的行为——更适合于派生类。

More generally, as it is stated in MSDN:

更一般地说,如MSDN所述:

The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

重写修饰符用于扩展或修改继承的方法、属性、索引器或事件的抽象或虚拟实现。