密封
应用于类时,sealed
修饰符可防止其他类继承它。
class A { }
sealed class B : A { }
class C : B { } //error : Cannot derive from the sealed class
当应用于 virtual
方法(或虚拟属性)时,sealed
修饰符会阻止在派生类中覆盖此方法(属性)。
public class A
{
public sealed override string ToString() // Virtual method inherited from class Object
{
return "Do not override me!";
}
}
public class B: A
{
public override string ToString() // Compile time error
{
return "An attempt to override";
}
}