C # Sealing class and sealing method--c# Foundation

Source: Internet
Author: User

1. Sealing type1) Not all classes can inherit, classes that cannot be inherited are called sealed classes. If a class does not want to be inherited, derived can take the keyword sealed, the following Class2 cannot inherit Class1Class1.cs:namespace sealing class and sealing method
{
//If a class does not want to be inherited, it can be derived using the keyword sealed
Public sealed class Class1
{
}
}Class2.csnamespace sealing class and sealing method
{
class class2:class1//cannot be inherited, Class1 has modifiers sealed
{
}
}2) Sealed classes cannot contain virtual methods (virtual) and abstract methods (abstract), and the existence of abstractions and virtual methods is intended to be inherited, and sealed classes do not have the opportunity to implement virtual methods and abstract methods for derived classes. 3). NET class libraries use sealed classes heavily, making it impossible for third-party developers who want to derive their classes from these classes from these classes, such as a string being a sealed class2. Sealing Method1) Sealing method: The method of marking with modifier sealed2) Role: Use sealed adornments to prevent derived classes from further overriding the method3) If the instance method declaration contains the sealed modifier, it must contain the override modifier4) cannot override the sealing method, example: (CLASS3 cannot override the Write () method in Class2)Class3.cs:class Class3:class2
{
//public override sealed void Write () {
//Console.WriteLine ("Write () This method can be rewritten");
//}
//cannot override sealing method
}Class2.cs:Public class Class2:class1
{
Public sealed override void Write ()
{
Console.WriteLine ("This is a sealing method");
}
}Class1.cs:public class Class1 {
public virtual void Write () {
Console.WriteLine ("This is an unsealed method");
}
}5) Source notes:Program.cs:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace sealing class and sealing method
{
The //sealed class cannot contain virtual methods (virtual) and abstract methods (abstract), and the existence of abstractions and virtual methods is used to inherit, and sealed classes do not have the opportunity to implement virtual and abstract methods for derived classes
//. NET class libraries use sealed classes heavily, making it impossible for third-party developers who want to derive their classes from these classes from these classes, such as a string being a sealed class
//Sealing Method: Method of marking with modifier sealed
//role: Use sealed adornments to prevent derived classes from further overriding the method
//If the instance method declaration contains the sealed modifier, it must contain the override modifier
//cannot override sealing method
//public override sealed void Wite () {
//Console.WriteLine ("Write () This method can be rewritten");
//}Class Program
{
static void Main (string[] args)
{
Class2 myClass2 = new Class2 ();
myclass2.write ();

}
}
}Class1.cs:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace sealing class and sealing method
{
//If a class does not want to be inherited, it can be derived using the keyword sealed
Public class Class1 {
Public virtual void Write () {
Console.WriteLine ("This is an unsealed method");
}
}
}Class2.cs:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace sealing class and sealing method
{
Public class Class2:class1
{
Public sealed override void Write ()
{
Console.WriteLine ("This is a sealing method");
}
}
}Class3.cs:Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.threading.tasks;namespace sealing class and sealing method
{
Not all classes can be inherited, and classes that cannot be inherited are called
//
Class Class3:class2
{
public override sealed void Write () {
Console.WriteLine ("Write () This method can be rewritten");
//}
Cannot override sealing method
}
}

C # Sealing class and sealing method--c# Foundation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.