简体   繁体   English

密封必须与覆盖一起使用?

[英]Sealed must be used with override?

From msdn sealed (C# Reference) 来自msdn sealed(C#参考)

"When applied to a method or property, the sealed modifier must always be used with override." “当应用于方法或属性时,必须始终使用sealed修饰符和override。”

Why must it always be used with override? 为什么必须始终使用覆盖?

sealed prevents a method from being overriden by subclasses. sealed可防止方法被子类覆盖。 If the method marked as sealed wasn't overridable in the first place, why would you mark it as sealed? 如果标记为密封的方法首先不能覆盖,为什么要将其标记为密封?

Because there is not reason to add it to a property that does not override a property from another class. 因为没有理由将它添加到不覆盖另一个类的属性的属性。 It you put the sealed modifier on a derived class's property, it is saying the anyone that derives from you cannot further override that property. 它将sealed修饰符放在派生类的属性上,它表示从您派生的任何人都无法进一步覆盖该属性。 If the property was never overridable to start with, there is no point in using sealed. 如果该属性从一开始就不会被覆盖,那么使用密封是没有意义的。

Basically, it is saying that subclasses must use the property the way you intended. 基本上,它表示子类必须按照您的预期方式使用该属性。

Because structs are implicitly sealed, they cannot be inherited, " sealed " prevents a method from being overriden by subclasses. 因为结构是隐式密封的,所以它们不能被继承,“ 密封 ”会阻止方法被子类覆盖。

See the example: In the following example, Z inherits from Y but Z cannot override the virtual function F that is declared in X and sealed in Y. 请参阅示例: In the following example, Z inherits from Y but Z cannot override the virtual function F that is declared in X and sealed in Y.

class X
{
    protected virtual void F() { Console.WriteLine("X.F"); }
    protected virtual void F2() { Console.WriteLine("X.F2"); }
}

Class Y inherits from class X, and define function F() as: sealed protected override void F(). 类Y继承自类X,并将函数F()定义为:sealed protected override void F()。

class Y : X
{
    sealed protected override void F() { Console.WriteLine("Y.F"); }
    protected override void F2() { Console.WriteLine("Y.F2"); }
}

Class Z that inherits from Y where function F() was defined as sealed, you can´t override the function because its defined as "sealed" 从Y继承的类Z,其中函数F()被定义为密封,你可以覆盖函数,因为它定义为“密封”

class Z : Y
{
    // Attempting to override F causes compiler error CS0239. 
    // protected override void F() { Console.WriteLine("C.F"); }

    // Overriding F2 is allowed. 
    protected override void F2() { Console.WriteLine("Z.F2"); }
}

more info: sealed (C# Reference) 更多信息: 密封(C#参考)

Say you have: 说你有:

public BaseClass
{
    public virtual void SomeMethod() 
    {
    }
}

And: 和:

public MyDerivedClass : BaseClass
{
     public void AnotherMethod()
     {
         // there's no point sealing this guy - it's not virtual
     }

     public override sealed void SomeMethod()
     {
         // If I don't seal this guy, then another class derived from me can override again
     }
}

And then: 然后:

public class GrandChildClass : MyDerivedClass
{
    public override void AnotherMethod()
    {
        // ERROR - AnotherMethod isn't virtual
    }

    public override void SomeMethod()
    {
        // ERROR - we sealed SomeMethod in MyDerivedClass
        // If we hadn't - this would be perfectly fine
    }
}

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

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