sealed(C# 参考)sealed (C# Reference)

应用于某个类时,sealed 修饰符可阻止其他类继承自该类。When applied to a class, the sealed modifier prevents other classes from inheriting from it. 在下面的示例中,类 B 继承自类 A,但没有类可以继承自类 BIn the following example, class B inherits from class A, but no class can inherit from class B.

class A {}
sealed class B : A {}

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

示例Example

在下面的示例中,Z 继承自 Y,但 Z 无法替代在 X 中声明并在 Y 中密封的虚函数 FIn 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 : 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"); }
}

在类中定义新方法或属性时,可以通过不将它们声明为虚拟,来防止派生类替代它们。When you define new methods or properties in a class, you can prevent deriving classes from overriding them by not declaring them as virtual.

abstract 修饰符与密封类结合使用是错误的,因为抽象类必须由提供抽象方法或属性的实现的类来继承。It is an error to use the abstract modifier with a sealed class, because an abstract class must be inherited by a class that provides an implementation of the abstract methods or properties.

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

因为结构是隐式密封的,所以无法继承它们。Because structs are implicitly sealed, they cannot be inherited.

有关详细信息,请参阅继承For more information, see Inheritance.

有关更多示例,请参阅抽象类、密封类及类成员For more examples, see Abstract and Sealed Classes and Class Members.

示例Example

sealed class SealedClass
{
    public int x;
    public int y;
}

class SealedTest2
{
    static void Main()
    {
        var sc = new SealedClass();
        sc.x = 110;
        sc.y = 150;
        Console.WriteLine($"x = {sc.x}, y = {sc.y}");
    }
}
// Output: x = 110, y = 150

在上面的示例中,可能会尝试使用以下语句从密封类继承:In the previous example, you might try to inherit from the sealed class by using the following statement:

class MyDerivedC: SealedClass {} // Error

结果是出现错误消息:The result is an error message:

'MyDerivedC': cannot derive from sealed type 'SealedClass'

备注Remarks

若要确定是否密封类、方法或属性,通常应考虑以下两点:To determine whether to seal a class, method, or property, you should generally consider the following two points:

  • 派生类通过可以自定义类而可能获得的潜在好处。The potential benefits that deriving classes might gain through the ability to customize your class.

  • 派生类可能采用使它们无法再正常工作或按预期工作的方式来修改类的可能性。The potential that deriving classes could modify your classes in such a way that they would no longer work correctly or as expected.

C# 语言规范C# language specification

有关详细信息,请参阅 C# 语言规范For more information, see the C# Language Specification. 该语言规范是 C# 语法和用法的权威资料。The language specification is the definitive source for C# syntax and usage.

请参阅See also