Easily learn the sealing class of C

  • 2021-08-31 08:56:53
  • OfStack

Overview of Sealing Classes
Not all classes can be inherited. In C #, they are sealed classes. In the C # language, we can declare classes as sealed. This means that the class cannot be inherited, and if you want to inherit, the compiler will definitely report an error. When a class or method is marked as sealed, the most likely scenario is that overriding some functionality will cause compilation errors if you want to operate on a library, class, or other class you write yourself. You can also mark a class or method as sealed for commercial reasons to prevent third parties from extending the class in a way that violates the registration agreement. However, in case 1, be careful when marking a class or method as sealed, because doing so will limit its use. Even if you don't want it to inherit a class or override a member of a class, it's still possible that at some point in the future, someone will encounter an unexpected situation. The. NET base class library makes extensive use of sealed classes, making them inaccessible to third-party developers who want to derive their own classes from these classes.
Sealed classes are declared in the following format:
Access modifier sealed class class name: base class or interface
{
Class body
}
Where access modifiers, base classes, or interfaces are optional.
Overview of sealing methods
When the instance method declaration contains the sealed modifier, the method is called a sealed method. If an instance method declaration contains an sealed modifier, it must contain an override modifier. Using the sealed modifier prevents derived classes from overriding this method in one step. To use the sealed keyword in a method, you must now declare it as an override in the base class. Do not declare an overridden method or property on the base class as virtual if you do not want it.
Access modifier sealed override method name (parameter list)
{
Method body
}
Where access modifiers and parameter lists are optional.
Let's take a look at an example


<span style="font-size:18px;">using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace qq 
{ 
  class class1 
  { 
    public virtual void seaText() 
    { 
      Console.WriteLine(" This is 1 An unsealed method! "); 
    } 
  } 
  sealed class class2 : class1 
  { 
    public sealed override void seaText() 
    { 
      Console.WriteLine(" This is 1 A sealing method! "); 
    } 
  } 
  /*class class3 : class2// Sealed classes cannot be inherited  
  { 
    public override void seaText()// You cannot override the sealing method  
    { 
      Console.WriteLine(" This is 1 A method that can't run! "); 
    } 
  }*/ 
  class Program 
  { 
    static void Main(string[] args) 
    { 
      class2 c2 = new class2(); 
      c2.seaText(); 
      Console.ReadLine(); 
    } 
  } 
}</span> 

The output result is very simple: this is a sealed method


<span style="font-size:18px;">using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace qq 
{ 
  sealed class Person 
  { 
    public void print_Person_name() 
    { 
      Console.WriteLine(" Zhang 3"); 
    } 
  } 
  class Student : Person// Sealed classes cannot be inherited  
  { 
  } 
  class Program 
  { 
    static void Main(string[] args) 
    { 
      Student s1 = new Student(); 
      s1.Print_Person_name();// You cannot call a method of a sealed class  
      Console.ReadLine(); 
    } 
  } 
}</span> 

This is an example of an error, in our use of sealed classes can not occur when such a situation, and is such a keyword we 1 generally want to use less.

The above is about C # sealed class of all the content of the introduction, I hope to help you learn.


Related articles: