简体   繁体   English

在 C# 中,对于基础 class 的虚拟/抽象方法,在派生 class 中,覆盖修饰符是强制性的

[英]In C# override modifier is mandatory in derive class for virtual/abstract methods of base class

public class Shapes
{
  public virtual void Color()
  {
    Console.WriteLine("Shapes");
  }
}

public class Square : Shapes
{
  public void Color()
  {
    Console.WriteLine("Square");
  }
}

public class Test
{ 
  public static void main()
  {
    Shapes s1 = new Shapes();
    s1.Color();
    Shapes s2 = new Square();
    s2.Color();
  }
}

I want to know if the base class has defined some methods virtual, then is it mandatory to override them in derive class?我想知道基础 class 是否定义了一些虚拟方法,那么在派生 class 中是否必须覆盖它们? Similarly, If the base class method is abstract, we need to implement the method in derive class, but is override modifier is mandatory?同样,如果基础 class 方法是抽象的,我们需要在派生 class 中实现该方法,但是覆盖修饰符是强制性的吗? What if override modifier is omitted.如果忽略了 override 修饰符会怎样。

I want to know if the base class has defined some methods virtual, then is it mandatory to override them in derive class?我想知道基础 class 是否定义了一些虚拟方法,那么在派生 class 中是否必须覆盖它们?

Definitely not.当然不。 It is up to the derived class to decide whether or not to override the virtual method.由派生的 class 决定是否覆盖虚拟方法。 Note that some classes will recommend that derived classes override a particular virtual method in some cases, but the compiler will not enforce that.请注意,某些类会建议派生类在某些情况下覆盖特定的虚拟方法,但编译器不会强制执行。

If the base class method is abstract, we need to implement the method in derive class, but is override modifier is mandatory?如果基础 class 方法是抽象的,我们需要在派生 class 中实现该方法,但是覆盖修饰符是强制性的吗?

Yes, you need to use the override keyword, otherwise the method will be hidden by the definition in the derived class.是的,需要使用override关键字,否则方法会被派生的class中的定义隐藏。

If you actually run your code, you will see "Shapes" printed twice.如果您实际运行您的代码,您将看到“形状”打印了两次。 That's because the Color() method in the Squares class isn't declared using the override keyword.这是因为Squares class 中的Color()方法没有使用override关键字声明。 As such, it hides the base class method.因此,它隐藏了基本 class 方法。 This means it will only be accessible through a variable of type Squares :这意味着它只能通过Squares类型的变量访问:

Sqares s3 = new Squares();
s3.Color();

This will print "Square", since you're calling the method on a variable of the correct type.这将打印“Square”,因为您在正确类型的变量上调用该方法。

Take a look at this link , it may be useful.看看这个链接,它可能有用。

A copy in that page:该页面中的副本:

To make a method virtual, the virtual modifier has to be used in the method declaration of the base class.要使方法虚拟化,必须在基础 class 的方法声明中使用virtual修饰符。 The derived class can then override the base virtual method by using the override keyword or hide the virtual method in the base class by using the new keyword.派生的 class 然后可以使用override关键字覆盖基本虚拟方法,或使用 new 关键字隐藏基本 class 中的虚拟方法。 If neither the override keyword nor the new keyword is specified, the compiler will issue a warning and the method in the derived class will hide the method in the base class如果未指定override关键字和new关键字,编译器将发出警告,派生 class 中的方法将隐藏基 class 中的方法

As you can see from the example code you posted, the output will be从您发布的示例代码中可以看出,output 将是

Shapes
Shapes

That is because by omitting the override keyword you are by default hiding the base class method - that means the method cannot be used in a polymorphic fashion anymore - even though s2 is an instance of Square since the object reference used to call the method is Shapes , the Shapes method is used.这是因为通过省略override关键字,您默认隐藏基本 class 方法 - 这意味着该方法不能再以多态方式使用 - 即使s2Square的一个实例,因为用于调用该方法的 object 引用是Shapes , 使用Shapes方法。

If the method is marked with override on the other hand it will be called even on object references of type Shape since the most derived Color() method in the inheritance tree for the given instance is used.另一方面,如果该方法被标记为override ,它甚至会在类型为Shape的 object 引用上调用,因为使用了给定实例的 inheritance 树中最派生的Color()方法。

It is completely based on your logic in first part of your question, In Square class which is derived from Shapes class you declare Color method which will hide its parent virtual Color method, you don't need to duplicate same method in child class when it exists in its parent, except when you want to hide parent class's method.它完全基于您在问题的第一部分中的逻辑,In Square class 派生自 Shapes class 您声明将隐藏其父虚拟 Color 方法的 Color 方法,您不需要在子 ZA2F2ED4F8EBC2CBB4C21A29DC4 中复制相同的方法存在于它的父类中,除非你想隐藏父类的方法。 In abstract class if you have a virtual method, you have to implement or override them in derived classes, in you sample those Shapes's instances will invoke virtual Color method in Shapes class.在抽象 class 中,如果您有一个虚拟方法,则必须在派生类中实现或覆盖它们,在您采样这些 Shapes 的实例时,将调用 Shapes class 中的虚拟 Color 方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM