Sealed method in C#

后端 未结 7 1674
萌比男神i
萌比男神i 2020-12-13 13:15

I am a newbie in C#.I am reading about Sealed keyword.I have got about sealed class.I have read a line about Sealed method where we can make Sealed method also.The line was

相关标签:
7条回答
  • 2020-12-13 13:42

    Well, you are testing with only two levels of inheritance, and you are not getting to the point that you're "further overriding" a method. If you make it three, you can see what sealed does:

    class Base {
       public virtual void Test() { ... }
    }
    class Subclass1 : Base {
       public sealed override void Test() { ... }
    }
    class Subclass2 : Subclass1 {
       public override void Test() { ... } // Does not compile!
       // If `Subclass1.Test` was not sealed, it would've compiled correctly.
    }
    
    0 讨论(0)
提交回复
热议问题