简体   繁体   English

覆盖密封有效,但为什么不在C#中进行虚拟密封?

[英]Override Sealed is valid but why not Virtual Sealed in C#?

The below code snippet is valid 以下代码段有效

public class BaseClass
{

   public virtual void Display()
   {
      Console.WriteLine("Virtual method");
   }
 }

public class DerivedClass : BaseClass
{

  public override sealed void Display()
  {
       Console.WriteLine("Sealed method");
  }

} }

But why not 但为什么不呢

public class BaseClass
{

 public virtual sealed void Display()
 {
          Console.WriteLine("Virtual method");
 }
}

Edited 编辑

Actually I was reading What is sealed class and sealed method? 其实我正在阅读什么是密封类和密封方法? this article. 本文。 So I was following the author's instruction. 所以我按照作者的指示。 Suddenly I tried to play the concept of Sealed with the base class. 突然,我试着用基类来演奏Sealed的概念。 That's why I came up with this question. 这就是我提出这个问题的原因。

override sealed is valid because it says "I'm overriding a base class method, but derived classes can't override me." override sealed是有效的,因为它说“我正在覆盖基类方法,但派生类不能覆盖我。” That makes sense. 那讲得通。 One part of it is talking about the relationship to its base class; 其中一部分是谈论与基类的关系; the other is talking about the relationship to derived classes. 另一个是谈论派生类的关系。

virtual sealed would be saying "You can override me ( virtual ) but you can't override me ( sealed )." virtual sealed将说“你可以覆盖我( virtual ),但你不能覆盖我( sealed )。” That makes no sense. 这是没有意义的。 The two modifiers are contradictory and apply to the same relationship. 这两个修饰语是矛盾的适用于相同的关系。

Sealed: 密封:

You can also use the sealed modifier on a method or property that overrides a virtual method or property in a base class. 您还可以在覆盖基类中的虚方法或属性的方法或属性上使用sealed修饰符。 This enables you to allow classes to derive from your class and prevent them from overriding specific virtual methods or properties. 这使您可以允许类派生自类,并防止它们覆盖特定的虚拟方法或属性。

Override: 覆盖:

The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event. override修饰符是扩展或修改继承方法,属性,索引器或事件的抽象或虚拟实现所必需的。

Sealed means it is the "end-of-the-hierarchy" for the given method/class. Sealed意味着它是给定方法/类的“层次结束”。 Making a method virtual is merely saying "this is the default behavior, override me if you want different behavior" and sealed which means "this cannot be overridden ever" is a paradox. 使virtual方法只是说“这是默认行为,如果你想要不同的行为,请覆盖我并且sealed意味着“这不能被覆盖”是一个悖论。

I'm not really sure what you'd expect that to even do. 我不确定你甚至会怎么想。

The sealed keyword means you can't inherit it. sealed关键字表示您无法继承它。

You can also use the sealed modifier on a method or property that overrides a virtual method or property in a base class. 您还可以在覆盖基类中的虚方法或属性的方法或属性上使用sealed修饰符。 This enables you to allow classes to derive from your class and prevent them from overriding specific virtual methods or properties. 这使您可以允许类派生自类,并防止它们覆盖特定的虚拟方法或属性。

The virtual keyword means you can. virtual关键字意味着你可以。

The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class. virtual关键字用于修改方法,属性,索引器或事件声明,并允许在派生类中重写它。 For example, this method can be overridden by any class that inherits it: 例如,任何继承它的类都可以覆盖此方法:

I'm not sure why you'd need or want both. 我不确定你为什么需要或想要两者。

When applying it to override , that makes sense. 将它应用于override ,这是有道理的。 That means "you can't override this behavior," and effectively overrules the previous (relative to the hierarchy) virtual keyword on the method or property. 这意味着“您无法覆盖此行为”,并且有效地取代了方法或属性上的先前(相对于层次结构) virtual关键字。

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

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