C#中sealed关键字

时间:2022-08-09 19:41:43

C#中sealed关键字

1. sealed关键字
    当对一个类应用 sealed 修饰符时,此修饰符会阻止其他类从该类继承。类似于Java中final关键字。     在下面的示例中,类 B 从类 A 继承,但是任何类都不能从类 B 继承。 2. sealed 修饰方法或属性
    能够允许类从基类继承,并防止它们重写特定的虚方法或虚属性。
    1)sealed是对虚方法或虚属性,也就是同override一起使用,如果不是虚方法或虚属性会报出错误:cannot be sealed because it is not an override
.publicclass D
.{
. /* ConsoleApplication1.MSFun.Sealed.D.M()'
4. * cannot be sealed because it is not an override
5. */
. public sealed void M() { Console.WriteLine("D.M()"); }
.}
public class D
{
/* ConsoleApplication1.MSFun.Sealed.D.M()'
* cannot be sealed because it is not an override
*/
public sealed void M() { Console.WriteLine("D.M()"); }
}

2)防止子类重写特定的方法或属性

.publicclass A
.{
. protectedvirtualvoid M() { Console.WriteLine("A.M()"); }
. protectedvirtualvoid M1() { Console.WriteLine("A.M1()"); }
.}
.
.publicclass B : A
.{
. protected sealed override void M() { Console.WriteLine("B.M()"); }
. protected override void M1() { Console.WriteLine("B.M1()"); }
.}
.
.public sealed class C : B
.{
. /* ConsoleApplication1.MSFun.Sealed.C.M()':
16. * cannot override inherited member 'ConsoleApplication1.MSFun.Sealed.B.M()'
17. * because it is sealed */
. //protected override void M() { Console.WriteLine("C.M()"); }
.
. protected override void M1() { Console.WriteLine("C.M1()"); }
.}
public class A
{
protected virtual void M() { Console.WriteLine("A.M()"); }
protected virtual void M1() { Console.WriteLine("A.M1()"); }
} public class B : A
{
protected sealed override void M() { Console.WriteLine("B.M()"); }
protected override void M1() { Console.WriteLine("B.M1()"); }
} public sealed class C : B
{
/* ConsoleApplication1.MSFun.Sealed.C.M()':
* cannot override inherited member 'ConsoleApplication1.MSFun.Sealed.B.M()'
* because it is sealed */
//protected override void M() { Console.WriteLine("C.M()"); } protected override void M1() { Console.WriteLine("C.M1()"); }
}