1. sealed keyword
when applied to a class sealed modifier, this modifier will prevent other classes inherit from this class. Similar to the final keyword in Java.
In the following example, a class can inherit B class A, but not inherit any class Class B
A {} class
Sealed class B: {A}
2. Sealed modification method or property
may allow for class inherits from the base class, and prevent them from overwriting virtual methods or specific virtual attributes.
1) sealed is a virtual method or virtual property, which is used together with the override, if not virtual methods or imaginary property will report an error: can not be sealed because it is not an override
public class A
{
protected virtual void M()
{
Console.WriteLine("A.M()");
}
protected virtual void N()
{
Console.WriteLine("A.N()");
}
}
public class B:A
{
protected override void M()
{
Console.WriteLine("B.M()");
}
protected sealed override void N()
{
Console.WriteLine("B.N()");
}
}
public sealed class C:B
{
protected override void M()
{
Console.WriteLine("C.M()");
}
protected override void N() //会报错 :"C.N():"继承成员"B.N()"是密封的,无法进行重写
{
Console.WriteLine ( "CN ()");
}
}
----------------
Disclaimer: This article is CSDN bloggers "- Li Jiang 'original article, follow the CC 4.0 BY -SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/qq_40323256/article/details/86771078