C#密封类介绍和用法示例

2021年5月10日16:16:00 发表评论 924 次浏览

密封类用于限制用户继承该类。可以使用密封关键词。关键字告诉编译器该类是密封的, 因此无法扩展。不能从密封类派生任何类。

以下是语法密封类:

sealed class class_name
{
    //data members
    //methods
    .
    .
    .

}

一种方法也可以密封, 在这种情况下, 该方法不能被覆盖。但是, 可以将方法密封在已继承它们的类中。如果要将方法声明为密封的, 则必须将其声明为虚拟在其基类中。

以下类定义在C#中定义了一个密封的类:

在下面的代码中, 创建一个密封的类密封等级并从程序中使用它。如果运行此代码, 则可以正常工作。

//C# code to define
//a Sealed Class
using System;
  
//Sealed class
sealed class SealedClass {
  
     //Calling Function
     public int Add( int a, int b)
     {
         return a + b;
     }
}
  
class Program {
  
     //Main Method
     static void Main( string [] args)
     {
  
         //Creating an object of Sealed Class
         SealedClass slc = new SealedClass();
  
         //Performing Addition operation
         int total = slc.Add(6, 4);
         Console.WriteLine( "Total = " + total.ToString());
     }
}

输出:

Total = 10

现在, 如果尝试从密封的类继承一个类, 则将产生一个错误, 指出"它不能从密封类.

//C# code to show restrictions
//of a Sealed Class
using System;
  
class Bird {
  
}
  
//Creating a sealed class
sealed class Test : Bird {
}
  
//Inheriting the Sealed Class
class Example : Test {
}
  
//Driver Class
class Program {
  
     //Main Method
     static void Main()
     {
     }
}

错误:

错误CS0509"示例":不能从密封类型"测试"派生

考虑以下派生类中的密封方法示例:

//C# program to
//define Sealed Class
using System;
  
class Printer {
  
     //Display Function for
     //Dimension printing
     public virtual void show()
     {
         Console.WriteLine( "display dimension : 6*6" );
     }
  
     //Display Function
     public virtual void print()
     {
         Console.WriteLine( "printer printing....\n" );
     }
}
  
//inherting class
class LaserJet : Printer {
  
     //Sealed Display Function
     //for Dimension printing
     sealed override public void show()
     {
         Console.WriteLine( "display dimension : 12*12" );
     }
  
     //Function to override
     //Print() function
     override public void print()
     {
         Console.WriteLine( "Laserjet printer printing....\n" );
     }
}
  
//Officejet class cannot override show
//function as it is sealed in LaserJet class.
class Officejet : LaserJet {
  
     //can not override show function or else
     //compiler error : 'Officejet.show()' : 
     //cannot override inherited member
     //'LaserJet.show()' because it is sealed.
     override public void print()
     {
         Console.WriteLine( "Officejet printer printing...." );
     }
}
  
//Driver Class
class Program {
  
     //Driver Code
     static void Main( string [] args)
     {
         Printer p = new Printer();
         p.show();
         p.print();
  
         Printer ls = new LaserJet();
         ls.show();
         ls.print();
  
         Printer of = new Officejet();
         of.show();
         of.print();
     }
}

输出:

display dimension : 6*6
Printer printing....

display dimension : 12*12
LaserJet printer printing....

display dimension : 12*12
Officejet printer printing....

说明:在上面的C#代码中, Printer类具有尺寸为6 * 6的显示单元, 而LaserJet类通过将其重写为12 * 12尺寸来实现了show方法。如果任何类将继承LaserJet类, 则它将具有12 * 12的相同尺寸, 并且无法实现自己的尺寸, 即不能具有15 * 15、16 * 16或任何其他尺寸。因此, LaserJet调用将密封show方法, 以防止进一步覆盖它。

为什么要上密封班?

  • 密封类用于阻止类被继承。你不能从中派生或扩展任何类。
  • 实施密封方法, 以便其他任何类都不能推翻它并实现自己的方法。
  • 密封类的主要目的是从用户撤回继承属性, 以使他们无法从密封类中获得一个类。当你的类具有静态成员时, 最好使用密封类。
    例如System.Drawing命名空间的"笔"和"画笔"类。 Pens类代表用于标准颜色的笔。该类只有静态成员。例如, " Pens.Red"代表红色的笔。同样, "画笔"类代表标准画笔。 " Brushes.Red"代表红色的画笔。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: