1.声明一个类不能被继承 类 B 继承自类 A,但没有类可以继承自类 B。
class A {} sealed class B : A {}
2.对替代基类中的虚方法或属性的方法或属性使用 sealed 修饰符。 这使你可以允许类派生自你的类并防止它们替代特定虚方法或属性。 Z 继承自 Y,但 Z 无法替代在 F 中声明并在 X 中密封的虚函数 Y
class X { protected virtual void F() { Console.WriteLine("X.F"); } protected virtual void F2() { Console.WriteLine("X.F2"); } } class Y : X { sealed protected override void F() { Console.WriteLine("Y.F"); } protected override void F2() { Console.WriteLine("Y.F2"); } } class Z : Y { // Attempting to override F causes compiler error CS0239. // protected override void F() { Console.WriteLine("Z.F"); } // Overriding F2 is allowed. protected override void F2() { Console.WriteLine("Z.F2"); } }
ealed(C# 参考)
There was an error while loading. Please reload this page.