laitimes

Object-Oriented Features (23) -- Sealed

author:chencalf

0, Reference

(1) It is better to speak: https://blog.csdn.net/XVJINHUA954/article/details/106876099

(2) Official address: https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/sealed

The following C# language explanation, description (I will talk about java later)

1. Functions and features

1.1 Features

The Chinese word for sealed means sealed, hence the implication, that is, the class or method modified by it cannot be inherited or overridden

1.2 Features

  • (1) The sealed modifier cannot be used at the same time as abstract, because the abstract class must be inherited by the class that provides the implementation of the abstract method or property, and the sealed class cannot be both an abstract class at the same time, because the abstract always wants to be inherited. Of course, sealed doesn't decorate the interface
  • (2) When a sealed modifier is applied to a method or property, the (sealed) sealed modifier must always be used in conjunction with (override) override (this refers to a rewrite of virtual)
  • (3) The sealed modifier can also improve some operational efficiency, because the inheritance class will override the member.

1.3 Role

  • The use of sealed in class declarations prevents other classes from inheriting this class,
  • Using the sealed modifier in the method declaration prevents the enrichment class from overriding this method,
  • The sealed modifier is primarily used to prevent unintentional derivation, but it can also prompt certain runtime optimizations.
  • There can never be any derivative class in a sealing class. If there is a virtual member function in the sealed class instance, the member function can be converted to a non-virtual one, and the function modifier virtual no longer takes effect.

2. Sealed test example

2.1 Example of a seal test

Note: Once a class is sealed, the entire class can no longer be inherited

(1) Basic usage

public sealed class XXX
{
//..............................
}           

(2) Simple examples

class a { }
sealed class b : a { }
//下面语句报错,无法从一个密封类继承
class c : b { }           

Cause

Object-Oriented Features (23) -- Sealed

2.2 Sealing method

Note: Once a method is sealed, it cannot be overridden. Correct usage: can only re-seal the virtual method, i.e. it can only be: sealed override XXX()

You can use the sealed modifier for a method, in which case we call the method a sealed method

  • Not every member method of a class can be used as a sealing method, and to be used as a sealing method, the virtual method of the parent class must be overridden to provide a concrete implementation method.
  • So, in method declarations, the sealed modifier is always used in conjunction with the override modifier.

(1) For the test example, if the method cannot be sealed directly, an error will be reported, as shown below

public class demoA
{
   //直接这么用会出错!!!
    public sealed void Run()
    {
        Console.WriteLine("--run---");
    }
}           

The following error message is displayed

Object-Oriented Features (23) -- Sealed

(2) Correct usage: It can be resealed by the virtual function, as shown below

1) Here's the right way to do it

public  class demoA
{
    public virtual void Run()
    {
        Console.WriteLine("-run--");
    }
}
public class demoB:demoA
{
    //正确使用,密封后,无法再重写
    public sealed override void Run()
    {
        Console.WriteLine("-run sealed--");
    }
}
class MasterClass
{
     public static void Main()
    {
        demoA demo = new demoB();
        demo.Run();
    }
}           

2) It cannot be inherited after sealing, otherwise an error will occur

public  class demoA
{
    public virtual void Run()
    {
        Console.WriteLine("-run--");
    }
}
public class demoB:demoA
{
    //正确使用,密封后,无法再重写
    public sealed override void Run()
    {
        Console.WriteLine("-run sealed--");
    }
}

public class demoC : demoB
{
    //因为demoB中该方法已经密封,无法继承
    public  override void Run()
    {
        Console.WriteLine("-run sealedCC--");
    }
}           

The error message is as follows

Object-Oriented Features (23) -- Sealed